tags:

views:

91

answers:

2

can we set the width of a view in android?

A: 

Actually I am placing an image as background in a view. I want to clip that image and the clipped image should fill the entire view. I am not getting it right.can you 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 matrix = new Matrix(); // // resize the bit map // matrix.postScale(100, 133); //
// Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap, 0, 0, 100, 133, matrix, true); // //
/*convert Bitmap to resource */ 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 is

android:layout_height="300dp" android:layout_width="300dp" android:id="@+id/img" android:layout_x="10dip" android:layout_y="50dip"

the problem is that the image is clipped and placed in the view but the clipped portion is still there. I was not able to resize the image to fit that view.Help me to solve this.Thankyou.

Hari
A: 

Add this to your xml

android:scaleType="fitXY"
Phobos