【发布时间】:2015-03-23 19:52:11
【问题描述】:
我有一个 imageview,在我在这个 imageview 上添加图像(标记)之后。 最后,我想创建一个位图,包括新的标记。 但是如果我尝试从相对布局创建位图,我会创建一个没有新标记的图像!为什么?
imageview= (ImageView)findViewById(R.id.image);
//insert a marker on my imageview
final RelativeLayout rr = (RelativeLayout) findViewById(R.id.relative);
rr.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
int x = (int) event.getX() ;
int y = (int) event.getY();
RelativeLayout.LayoutParams lp =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);; //Assuming you use a RelativeLayout
ImageView iv=new ImageView(getApplicationContext());
lp.setMargins(x,y,0,0);
iv.setLayoutParams(lp);
iv.setImageDrawable(getResources().getDrawable(R.drawable.marker));
((ViewGroup)v).addView(iv);
//create a bitmap from relative layout but the new bitmap is without marker
Bitmap b1 = Bitmap.createBitmap(rr.getWidth(), rr.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b1);
((ViewGroup)v).draw(c);
}
return false;
}
【问题讨论】:
-
发生这种情况是因为您正在向布局动态添加标记,因为添加动态视图有一些延迟。这就是创建没有标记的位图的原因。在第二次触摸时,将创建单个标记位图。
-
好的,谢谢。但是我该如何解决这个问题呢?
-
我解决了这个问题!我创建了一个变量“count”,在第二次触摸时不会添加标记,而是保存位图!并且在第一次触摸之后,吐司显示触摸屏幕给用户确认。感谢您的建议。
-
下面给出了另一个更好的解决方案。调查一下
标签: android image bitmap imageview android-relativelayout