Java 添加一个图像作为背景,并将其他图像置于其上
我正在使用画布和位图类创建一个图像。我想将其设置为用户的背景。然后我想在上面再添加一些图像 这是应该作为背景的图像的代码Java 添加一个图像作为背景,并将其他图像置于其上,java,android,image,background,Java,Android,Image,Background,我正在使用画布和位图类创建一个图像。我想将其设置为用户的背景。然后我想在上面再添加一些图像 这是应该作为背景的图像的代码 ImageView imgMap1 = (ImageView) findViewById(R.id.imgMap1); imgMap1.setImageDrawable(new BitmapDrawable(Bitmap.createBitmap(bmp, 0, 0, 500, 500))); 这是作为背景的代码: LinearLayout ll = new LinearL
ImageView imgMap1 = (ImageView) findViewById(R.id.imgMap1);
imgMap1.setImageDrawable(new BitmapDrawable(Bitmap.createBitmap(bmp, 0, 0, 500, 500)));
这是作为背景的代码:
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundResource(R.drawable.nn);
this.setContentView(ll);
这里的问题是:当我将它设置为背景时,我再也看不到另一张照片了。
我该怎么做?
提前谢谢
其他图像将添加到布局中。它们可以用手指触摸移动。用户可以用手指重新定位它们
ImageView i1 = (ImageView) findViewById(R.id.Image1);
ImageView i2 = (ImageView) findViewById(R.id.Image2);
布局是在XML文件中自上而下构建的,或者按照您在代码中添加元素的顺序构建的。听起来像是其他图像首先被添加到布局中,或者作为XML文件中更高的元素,或者作为代码中更早的元素。您需要添加其他代码作为完整答案的上下文。刚刚注意到,您可以使用
setImageBitmap(Bitmap bm)
然后让我们谈谈你的问题
首先,创建自己的类扩展视图
;
其次,使用位图加载背景图像和覆盖图像
第三,调用onTouch
事件,以便onDraw
方法将使用onTouch
返回的坐标自动重新绘制覆盖图像
比如:
public class dragndrop extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
// using this to load your background image
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565; // this is not a must
bmBackground = BitmapFactory.decodeFile(filePath, opt);
super.onCreate(savedInstanceState);
DrawView dv = new DrawView(dragndrop.this);
setContentView(dv);
}
public class DrawView extends View {
Point coordinate;
public DrawView(Context context) {
super(context);
setFocusable(true); //necessary for getting the touch events
}
@Override
protected void onDraw(Canvas canvas) {
// assume you have already load your background image as bitmap
canvas.drawBitmap(bmBackground, 0, 0, null);
// assume bm is the overlay image you need to put on top,
// the method here will draw the object with the coordinate you give
canvas.drawBitmap(bm, coordinate.x, coordinate.y, null);
}
// events when touching the screen
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int x = (int)event.getX();
int y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN:
// add code here to determine the point user touched is within the object or not
break;
}
}
break;
case MotionEvent.ACTION_MOVE: // touch 'n' drag
// pass the touch point to your object
coordinate.x = x;
coordinate.y = y;
break;
case MotionEvent.ACTION_UP:
// touch drop - just do things here after dropping
break;
}
// redraw the canvas
invalidate();
return true;
}
}
}
这是我自己的代码片段,请在使用前编辑,问题解决后请告诉我。您可以发布更多关于如何添加其他图像的代码吗?您可以在第一个图像上绘制第二个图像。看看我的答案,你是如何做到这一点的:谢谢你的答案。代码被添加。我有两种可能性。我可以在布局中添加它们,也可以在java代码中添加它们。哪一个可以工作?@viperbone:我看到了你的代码,但这是一个imageview,可以使用OnDragListener移动。你还有其他想法吗?这个解决方案和我的编码有点不同,所以我对此一无所知。我会试试看,并报告结果。