【问题标题】:how to get the bitmap from cusom imageview class?如何从 cusom imageview 类中获取位图?
【发布时间】:2014-05-04 07:43:44
【问题描述】:

我想要获取自定义类的整个 bimap 地图...我得到空值...我尝试各种方法我没有得到写答案..

Bitmap b = mBoardTile.getDrawing();我用了它但是得到了空值..

我也使用了视图缓存,例如..

      Bitmap b = null;
    try {
        mBoardTile.setDrawingCacheEnabled(true);
        mBoardTile.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        mBoardTile.layout(0, 0, mBoardTile.getMeasuredWidth(), mBoardTile.getMeasuredHeight());
        mBoardTile.buildDrawingCache(true);
        b = mBoardTile.getDrawingCache();
    } catch (Exception e) {
        e.printStackTrace();
    }`

但再次获得空值..

自定义类在下面..

public class BoardTile extends ImageView {
 Context mContext;
 int posx, posy;
 ArrayList<Datavo> mArrayListDta;

float width, height, newx, newy;
ArrayList<Datavo> mArrayListNew;

public BoardTile(Context context) {
    super(context);
    this.mContext = context;
    mArrayListNew = new ArrayList<Datavo>();
}

@Override
protected void onDraw(Canvas canvas) {
    for (int i = 0; i < mArrayListNew.size(); i++) {
        Datavo mDatavo = mArrayListNew.get(i);

                Bitmap mOriginalBitmap = mDatavo.getmBitmap();
                // Scale to target size
                mOriginalBitmap = Bitmap.createScaledBitmap(mOriginalBitmap, mDatavo.getWidth_new(), mDatavo.getHeight_new()/2, true);

   //                   Canvas mCanvas = new Canvas(mOriginalBitmap);
                canvas.drawBitmap(mOriginalBitmap, 0, 0, null);
    }

}

public void getData(ArrayList<Datavo> mArrayList) {

    this.mArrayListDta = mArrayList;
    for (int i = 0; i < mArrayListDta.size(); i++) {

        newx = 480 * mArrayListDta.get(i).getxCordi() / mArrayListDta.get(i).getWidth();
        newy = 800 * mArrayListDta.get(i).getyCordi() / mArrayListDta.get(i).getHeight();

        width = newx * mArrayListDta.get(i).getWidth() / mArrayListDta.get(i).getxCordi();
        height = newy * mArrayListDta.get(i).getHeight() / mArrayListDta.get(i).getyCordi();

        Datavo mDatavo = new Datavo();
        mDatavo.setxCordi_new((int) newx);
        mDatavo.setyCordi_new((int) newy);
        mDatavo.setWidth_new((int) width);
        mDatavo.setHeight_new((int) height);
        mDatavo.setmBitmap(mArrayListDta.get(i).getmBitmap());
        mArrayListNew.add(mDatavo);

    }
}
}

【问题讨论】:

  • 你为什么在 onDraw 方法中这样做它会调用多次..
  • @kalyanpvs 我在 getdata() 数组列表中添加了多个图像视图.. 所以我需要 for 循环..
  • @kalyanpvs 我只是添加了多个图像视图并将其全部绘制在画布上.. 它的绘制完美但没有获取自定义类图像视图位图..
  • 不要在onDraw()回调中做繁重的工作。这里createScaledBitmap() 是一项昂贵的工作......
  • @GopalRao 我知道但没有另一种方法来处理所有尺寸合适的图像位图..

标签: android canvas bitmap android-custom-view


【解决方案1】:
BitmapDrawable bitmapDrawable=(BitmapDrawable) _image_view.getDrawable();
bitmapDrawable.getBitmap()

【讨论】:

    【解决方案2】:

    我得到了从自定义类中获取整个位图的解决方案......

    public static Bitmap getBitmapFromView(View view) {
        //Define a bitmap with the same size as the view
    
        int FixWidth = 480;
        int FixHeight = 800;
        int bit_width = 0;
        int bit_height = 0;
    
        bit_width = mImageViewBackgroud.getDrawable().getIntrinsicWidth();
        bit_height = mImageViewBackgroud.getDrawable().getIntrinsicHeight();
    
        if (bit_width > FixWidth) {
            bit_width = FixWidth;
        }
    
        if (bit_height > FixHeight) {
            bit_height = FixHeight;
        }
    
        float ratio = Math.min((float) 800 / bit_width, (float) 800 / bit_height);
        int width = Math.round((float) ratio * bit_width);
        int height = Math.round((float) ratio * bit_height);
    
        Bitmap returnedBitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
        //Bind a canvas to it
        Canvas canvas = new Canvas(returnedBitmap);
        //Get the view's background
        Drawable bgDrawable =view.getBackground();
        if (bgDrawable!=null) 
            //has background drawable, then draw it on the canvas
            bgDrawable.draw(canvas);
        else 
            //does not have background drawable, then draw white background on the canvas
            canvas.drawColor(Color.WHITE);
        // draw the view on the canvas
        view.draw(canvas);
        //return the bitmap
        return returnedBitmap;
    }
    

    希望此代码对其他人有所帮助....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      相关资源
      最近更新 更多