如何从cusom imageview类中获取位图?

时间:2014-03-26 05:07:15

标签: android canvas bitmap android-custom-view

我希望获得自定义类的整个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);

    }
}
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

希望这段代码有助于其他人......