I am creating an image by using Canvas- and Bitmap class. I want to set it as a background for the user. Then I want to add some more images on top of it.
我正在使用Canvas-和Bitmap类创建一个图像。我想将其设置为用户的背景。然后我想在它上面添加更多图像。
this is the code for image that is supposed to be as background.
这是应该作为背景的图像代码。
ImageView imgMap1 = (ImageView) findViewById(R.id.imgMap1);
imgMap1.setImageDrawable(new BitmapDrawable(Bitmap.createBitmap(bmp, 0, 0, 500, 500)));
and this is the code to make it as background:
这是使其成为背景的代码:
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundResource(R.drawable.nn);
this.setContentView(ll);
The Problem here is: When I set it as background, I can't see the other photo anymore. How can I do this? Thanks in advance.
这里的问题是:当我将其设置为背景时,我再也看不到另一张照片了。我怎样才能做到这一点?提前致谢。
The other Images are added in the Layout. they are movable by finger touch. user can reposition them by finger.
其他图像将添加到布局中。它们可以通过手指触摸移动。用户可以通过手指重新定位它们。
ImageView i1 = (ImageView) findViewById(R.id.Image1);
ImageView i2 = (ImageView) findViewById(R.id.Image2);
2 个解决方案
#1
0
The layout is built from the top down in your XML file, or in the order you add elements in code. It sounds like your other images is being added to the layout first, either as a higher-up element in the XML file, or earlier in your code. You'll need to add the other code as context for a complete answer.
布局是从XML文件中自上而下构建的,或者是在代码中添加元素的顺序。听起来您的其他图像首先被添加到布局中,或者作为XML文件中的更高元素,或者在代码中更早。您需要将其他代码添加为完整答案的上下文。
#2
0
Just noticed that you can directly set Bitmap
as your ImageView
's content using setImageBitmap(Bitmap bm)
See the Android Reference
刚刚注意到你可以使用setImageBitmap直接将Bitmap设置为ImageView的内容(Bitmap bm)请参阅Android参考资料
Then let talk about your question.
然后谈谈你的问题。
First, create your own class extends the View
; Second, load background image and overlay image using Bitmap Third, invoke onTouch
event, so that the onDraw
method will automatically redraw the overlay image using the coordinates returned by onTouch
首先,创建自己的类扩展View;二,使用Bitmap加载背景图像和叠加图像三,调用onTouch事件,以便onDraw方法将使用onTouch返回的坐标自动重绘叠加图像
Something like:
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;
}
}
}
This is my own snippet, please edit before use, and please let me know when your question is resolved.
这是我自己的代码片段,请在使用前进行编辑,如果您的问题得到解决,请与我们联系。
#1
0
The layout is built from the top down in your XML file, or in the order you add elements in code. It sounds like your other images is being added to the layout first, either as a higher-up element in the XML file, or earlier in your code. You'll need to add the other code as context for a complete answer.
布局是从XML文件中自上而下构建的,或者是在代码中添加元素的顺序。听起来您的其他图像首先被添加到布局中,或者作为XML文件中的更高元素,或者在代码中更早。您需要将其他代码添加为完整答案的上下文。
#2
0
Just noticed that you can directly set Bitmap
as your ImageView
's content using setImageBitmap(Bitmap bm)
See the Android Reference
刚刚注意到你可以使用setImageBitmap直接将Bitmap设置为ImageView的内容(Bitmap bm)请参阅Android参考资料
Then let talk about your question.
然后谈谈你的问题。
First, create your own class extends the View
; Second, load background image and overlay image using Bitmap Third, invoke onTouch
event, so that the onDraw
method will automatically redraw the overlay image using the coordinates returned by onTouch
首先,创建自己的类扩展View;二,使用Bitmap加载背景图像和叠加图像三,调用onTouch事件,以便onDraw方法将使用onTouch返回的坐标自动重绘叠加图像
Something like:
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;
}
}
}
This is my own snippet, please edit before use, and please let me know when your question is resolved.
这是我自己的代码片段,请在使用前进行编辑,如果您的问题得到解决,请与我们联系。