我寫了代碼來繪製視圖。完成後,如何從視圖中獲取生成的圖像。例如,在下面的代碼中,我想從mCustomDrawableView中獲取drawable(Image)。我怎樣才能做到這一點?謝謝。如何從視圖中檢索drawable(圖片)?
public class HelloTestGraph extends Activity {
/** Called when the activity is first created. */
// @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout lo = (LinearLayout) findViewById(R.id.top_view);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
CustomDrawableView mCustomDrawableView = new CustomDrawableView(this);
mCustomDrawableView.setLayoutParams(param);
lo.addView(mCustomDrawableView);
}
public class CustomDrawableView extends View {
private ShapeDrawable mDrawable;
private Drawable mPic;
public CustomDrawableView(Context context) {
super(context);
int x = 10;
int y = 10;
int width = 300;
int height = 50;
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
mPic = getResources().getDrawable(R.drawable.example_picture);
mPic.setBounds(x, y + 100, x + width, y + height+100);
}
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
mPic.draw(canvas);
}
}
}
你的做法是對的。我也在網上搜索,發現我可以使用下面的代碼來做到這一點: Bitmap image = Bitmap.createBitmap(mCustomDrawableView.getWidth(),mCustomDrawableView.getHeight(),Bitmap.Config.RGB_565); mCustomDrawableView.draw(new Canvas(image)); – user256239 2010-03-03 21:43:28