【问题标题】:Canvas in the middle of Layout i.e ImageView Android?布局中间的画布,即 ImageView Android?
【发布时间】:2015-04-02 16:33:03
【问题描述】:

我正在寻找在 xml 布局中间显示画布的任何方式,即在 Imageview 上显示画布。我试过但位图返回null。

Bitmap b = Bitmap.createBitmap(img.getMeasuredWidth(),img.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.drawRect(0, 0, 200, 200, paint);

【问题讨论】:

  • xml 布局中间的画布?你什么意思?
  • 意思是简单的布局,中间有一个图像视图,并在上面放置一个画布

标签: java android android-layout android-activity bitmapimage


【解决方案1】:

请子类化 ImageView 并在 onDraw() 中绘制您的要求,如下所示。

public class CustomImageView extends ImageView{

public CustomImageView (Context context) {
    super(context);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Bitmap Bitmap.createBitmap(img.getMeasuredWidth(),img.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.drawRect(0, 0, 200, 200, paint);
}

}

【讨论】:

    【解决方案2】:

    使用此代码创建位图。

     public static Bitmap createScaledBitmap(String path, float scale, boolean filtering){
            Bitmap src = BitmapFactory.decodeFile(path);
            int width = (int)( src.getWidth() * scale + 0.5f);
            int height = (int)( src.getHeight() * scale + 0.5f);
            return Bitmap.createScaledBitmap(src, width, height, filtering);
          }
    

    这里的路径是你的图像的路径,然后最后把这个缩放的位图放在你的画布中。

    【讨论】:

      猜你喜欢
      • 2014-03-05
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      相关资源
      最近更新 更多