2014-03-26 84 views
2

我希望获得定制类的全bimap的地图...我得到空...我尝试一切办法我也没有得到答案写..如何从cusom imageview类获取位图?

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

IM也用视图缓存如..

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

    } 
} 
} 
+0

你为什么做它onDraw方法它会多次调用.. –

+0

@kalyanpvs我在getdata()arraylist ..中添加多个imageview ..所以我需要for循环.. –

+0

@kalyanpvs我只是添加多个imageview和它的所有绘制在画布上..它画出官员,但没有得到自定义类imageview位图.. –

回答

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

谢谢,但不工作..我得到空值 –

1

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

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

希望这个代码可以帮助别人....