简体   繁体   English

如何从cusom imageview类获取位图?

[英]how to get the bitmap from cusom imageview class?

i want the get the whole bimap map of custom class... i getting the null...i try to every way i don't getting the write answer.. 我想要获取自定义类的整个bimap映射...我获取null ...我尝试各种方式我都没有得到写答案..

Bitmap b = mBoardTile.getDrawing(); i used it but get the null value.. 我用它,但得到空值。

im also used view cache like.. 我还用过视图缓存之类的..

      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();
    }`

but again get the null value.. 但再次获得空值。

the custom class is below.. 自定义类如下。

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);

    }
}
}
BitmapDrawable bitmapDrawable=(BitmapDrawable) _image_view.getDrawable();
bitmapDrawable.getBitmap()

i got the solution for getting the whole bitmap from custom class.... 我得到了从自定义类获取整个位图的解决方案。

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;
}

hope this code helps to others.... 希望这段代码对其他人有帮助...。

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

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