繁体   English   中英

在android中设置视图宽度

[英]Set width of view in android

我们可以在android中设置视图的宽度吗?

实际上我在视图中将图像作为背景放置。 我想剪切该图像,剪切的图像应该填满整个视图。 我没有把它弄好。你帮忙。 这是我的代码....

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(); // //调整位图大小// matrix.postScale(100,133); //
//位图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());
  }

//我的main.xml是

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

问题是图像被剪裁并放置在视图中,但剪切部分仍然存在。 我无法调整图像大小以适应该视图。帮助我解决这个问题.Thankyou。

将其添加到您的xml中

android:scaleType="fitXY"

是的你可以。只需要布局android:layout_width =“300dip”设置你可以根据自己的要求进行设置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM