Set view width in android

can I set the width of the view in android?

+3
source share
3 answers

I actually put the image as the background in the view. I want to pin this image, and the cropped image should fill the whole view. I don’t get it right. You can help. here is my code ....

try {setContentView (R.layout.main); ImageView img = (ImageView) findViewById (R.id.img);

        Paint paint = new Paint();
        paint.setFilterBitmap(true);        
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.puli);

        int targetWidth  = 300;
        int targetHeight = 300;


        Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888);

       // System.out.println(" P  ointsArrayX =" + PointsArrayX[i-1] + " PointsArrayY =" + PointsArrayY[i-1]);

        RectF rectf = new RectF(0, 00, 100, 100);

        Canvas canvas = new Canvas(targetBitmap);
        Path path = new Path();               

        path.addRect(rectf, Path.Direction.CW);
        canvas.clipPath(path);

        canvas.drawBitmap( bitmapOrg,new Rect(0, 0, bitmapOrg.getWidth(), bitmapOrg.getHeight()),
                        new Rect(0, 0, targetWidth, targetHeight), null);

// = (); //// //matrix.postScale(100, 133); //
//Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap, 0, 0, 100, 133, matrix, true); ////
          /* Bitmap */           BitmapDrawable bd = new BitmapDrawable (targetBitmap);

        AbsoluteLayout.LayoutParams abs_params = 
            new AbsoluteLayout.LayoutParams(
                //width in pixels
                30,
                //height in pixels
                40, 0, 0
            );

        img.setLayoutParams(abs_params);
        img.setImageDrawable(bd);


    img.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            System.out.println("onTouch");
            return false;
        }
    });

    }
    catch(Exception e){
        System.out.println("Error1 : " + e.getMessage() + e.toString());
  }

//my main.xml

      : layout_height = "300dp"       : layout_width = "300dp"       : = "@+ /IMG "       : layout_x = "10dip"       : layout_y = "50dip"

, , . . . .

+1

xml

android:scaleType="fitXY"
+1

Yes, you can. Just put the android layout: layout_width = "300dip" Put as your requirement.

+1
source

Source: https://habr.com/ru/post/1767998/


All Articles