如何移动单帆布的android多个位图 [英] How to move multiple bitmaps in single canvas android
本文介绍了如何移动单帆布的android多个位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要在同一个画布上移动多个位图。使用下面code,我可以移动一个位图时,屏幕上的触摸,但我不能确定的位图的触摸事件,让我动弹不得具体的位图。
公共类DrawTopologyView扩展视图{
涂料粉刷=新的油漆();
位图zed_bitMap,lamp_on_bitmap,fan_on_bitmap,ac_on_bitmap;INT START_X = 5;
INT START_Y = 5;浮动X = 500-24,Y = START_Y;公共DrawTopologyView(上下文的背景下){
超级(上下文);
zed_bitMap = BitmapFactory.de codeResource(context.getResources()
R.drawable.zed);
lamp_on_bitmap = BitmapFactory.de codeResource(context.getResources()
R.drawable.lamp_on);
fan_on_bitmap = BitmapFactory.de codeResource(context.getResources()
R.drawable.fan_on);
ac_on_bitmap = BitmapFactory.de codeResource(context.getResources()
R.drawable.ac_on);}@覆盖
公共无效的onDraw(帆布油画){ INT CENTER_X = canvas.getWidth()/ 2 - 24;
INT CENTER_Y = canvas.getHeight()/ 2 - zed_bitMap.getHeight(); INT FULL_WIDTH = canvas.getWidth();
INT FULL_HEIGHT = canvas.getHeight(); canvas.drawBitmap(zed_bitMap,CENTER_X,CENTER_Y,油漆);
paint.setStrokeWidth(3);
paint.setPathEffect(新DashPathEffect(新浮法[] {5,5,5,5},0)); canvas.drawBitmap(ac_on_bitmap,START_X,START_Y,油漆);
canvas.drawLine(START_X + 48,+ START_Y 48 CENTER_X + 10,
CENTER_Y + 10,油漆); canvas.drawLine(250,START_Y + 48,CENTER_X + 10,CENTER_Y + 10,油漆);
canvas.drawBitmap(lamp_on_bitmap,250 - 24,START_Y,油漆); canvas.drawLine(500,START_Y + 48,CENTER_X + 10,CENTER_Y + 10,油漆);
canvas.drawBitmap(fan_on_bitmap,X,Y,油漆);
// canvas.drawLine(FULL_WIDTH-40,FULL_HEIGHT-120,CENTER_X + 30,CENTER_Y + 30,
//油漆);
// canvas.drawBitmap(light_off_bitmap,FULL_WIDTH-60-24,FULL_HEIGHT-130,
//油漆);}公共布尔onTouchEvent(MotionEvent事件){ 开关(event.getAction()){
案例MotionEvent.ACTION_DOWN:{ }
打破; 案例MotionEvent.ACTION_MOVE:{
X =(int)的event.getX();
Y =(INT)event.getY(); 无效();
} 打破;
案例MotionEvent.ACTION_UP: X =(int)的event.getX();
Y =(INT)event.getY();
Log.d(APPConstant.LOG_TAG..................+ X +......+ Y);
Toast.makeText(的getContext(),..................+ X +......+ Y,Toast.LENGTH_SHORT).show() ;
无效();
打破;
}
返回true;
}}
解决方案
公共类SimpleDrag扩展视图{私人最终诠释INVALID_INDEX = -1;私人最终诠释mTotalItems = 5;私人的ArrayList<矩形> mItemsCollection;私人的ArrayList<点和GT; mActiveDragPoints;私人的ArrayList<矩形> mActiveRects;
私人涂料mPaint;/ **
* @参数方面
*类型@返回SimpleDrag
*构造函数
* @since 2013年2月19日
* @author rajeshcp
* /
公共SimpleDrag(上下文的背景下){
超级(上下文);
在里面();
}/ **
* @参数方面
* @参数ATTRS
*类型@返回SimpleDrag
*构造函数
* @since 2013年2月19日
* @author rajeshcp
* /
公共SimpleDrag(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
在里面();
}/ **
* @参数方面
* @参数ATTRS
* @参数defStyle
*类型@返回SimpleDrag
*构造函数
* @since 2013年2月19日
* @author rajeshcp
* /
公共SimpleDrag(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
超(背景下,ATTRS,defStyle);
在里面();
}/ *(非Javadoc中)
* @see android.view.View#的onDraw(android.graphics.Canvas)
* @since 2013年2月19日
* @author rajeshcp
* /
@覆盖
保护无效的onDraw(帆布油画){
super.onDraw(画布);
canvas.drawColor(Color.BLUE,PorterDuff.Mode.CLEAR);
对于(矩形RECT:mItemsCollection)
{
canvas.drawRect(RECT,mPaint);
}
}
/ **
*空类型@参数
*类型为null @返回
*函数将初始化视图
* @since 2013年2月20日
* @author rajeshcp
* /
私人无效的init()
{
mActiveRects =新的ArrayList<矩形>(mTotalItems);
mActiveDragPoints =新的ArrayList<点和GT;(mTotalItems);
mItemsCollection =新的ArrayList<矩形>();
的for(int i = 0; I< mTotalItems;我++)
{
矩形矩形=新的Rect(ⅰ* 100,我* 100,第(i + 1)* 100,第(i + 1)* 100);
mItemsCollection.add(RECT);
}
mPaint =新的油漆(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.RED);
}/ *(非Javadoc中)
* @see android.view.View#onTouchEvent(android.view.MotionEvent)
* @since 2013年2月19日
* @author rajeshcp
* /
@覆盖
公共布尔onTouchEvent(MotionEvent事件){ 最终诠释行动= event.getActionMasked();
最终诠释指针= event.getActionIndex(); 开关(动作){
案例MotionEvent.ACTION_DOWN:
点触地得分=新的点((INT)event.getX(),(INT)event.getY());
lookForIntersection(着陆);
打破;
案例MotionEvent.ACTION_UP:
案例MotionEvent.ACTION_CANCEL:
mActiveDragPoints.removeAll(mActiveDragPoints);
mActiveRects.removeAll(mActiveRects);
打破;
案例MotionEvent.ACTION_MOVE:
诠释计数= 0;
对于(矩形RECT:mActiveRects)
{
点curretPoint =新的点((INT)event.getX(计数),(INT)event.getY(计数));
moveRect(curretPoint,mActiveDragPoints.get(计数),RECT);
算上++;
}
Log.d(的getClass()的getName(),活性Rects+ mActiveRects.size());
Log.d(的getClass()的getName(),活性点+ mActiveDragPoints.size());
无效();
打破;
案例MotionEvent.ACTION_POINTER_DOWN:
着陆=新的点((INT)event.getX(指针),(INT)event.getY(指针));
lookForIntersection(着陆);
。//Log.d(getClass()的getName(),ACTION_POINTER_DOWN+指针);
打破;
案例MotionEvent.ACTION_POINTER_UP:
INT指数= getIntersectionRectIndex(新点((int)的event.getX(指针),(INT)event.getY(指针)));
如果(指数!= INVALID_INDEX)
{
RECT RECT = mItemsCollection.get(指数);
mActiveDragPoints.remove(mActiveRects.indexOf(矩形));
mActiveRects.remove(RECT);
} 打破; 默认:
打破;
}
返回true;
}
/ **
* @参数类型的点触地
*类型为null @返回
*功能,就会发现
*相交的矩形,并加入到
*活动收集
* @since 2013年2月20日
* @author rajeshcp
* /
私人无效lookForIntersection(着陆点)
{
最终诠释指数= getIntersectionRectIndex(着陆); 如果(指数!= INVALID_INDEX)
{
最后矩形RECT = mItemsCollection.get(指数);
如果(mActiveRects.indexOf(矩形)== INVALID_INDEX)
{
mActiveDragPoints.add(着陆);
mActiveRects.add(mItemsCollection.get(指数));
}
}
Log.d(的getClass()的getName(),活性Rects+ mActiveRects.size());
Log.d(的getClass()的getName(),活性点+ mActiveDragPoints.size());}
/ **
* @参数类型为Point点
* int类型的@return
*函数将返回的索引
*在RECT contaning给定点
* @since 2013年2月20日
* @author rajeshcp
* /
私人诠释getIntersectionRectIndex(最终点对点)
{
INT指数= INVALID_INDEX;
对于(矩形RECT:mItemsCollection)
{
如果(rect.contains(point.x,point.y))
{
指数= mItemsCollection.indexOf(RECT);
打破;
}
}
返回指数;
}
/ **
* @参数类型点currentPoint
Point类型的* @参数prevPoint
* @参数类型矩形的RECT
*类型为null @返回
*将移动变化功能
*德RECT的界限
* @since 2013年2月20日
* @author rajeshcp
* /
私人无效moveRect(点currentPoint,点prevPoint,最终矩形RECT)
{
INT xMoved = currentPoint.x - prevPoint.x;
INT yMoved = currentPoint.y - prevPoint.y;
rect.set(rect.left + xMoved,rect.top + yMoved,rect.right + xMoved,rect.bottom + yMoved);
mActiveDragPoints.set(mActiveDragPoints.indexOf(prevPoint),currentPoint);
}}
这允许您一次选择多个位为好。我只使用矩形您可以填写此这种情况下矩形与位图。
I want to move multiple bitmap on the same canvas. Using below code i can move one bitmap when touch on the screen but, i can't identify the touch event on the bitmap so that i couldn't move specific bitmap.
public class DrawTopologyView extends View {
Paint paint = new Paint();
Bitmap zed_bitMap, lamp_on_bitmap, fan_on_bitmap, ac_on_bitmap;
int START_X = 5;
int START_Y = 5;
float x = 500-24,y=START_Y;
public DrawTopologyView(Context context) {
super(context);
zed_bitMap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.zed);
lamp_on_bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.lamp_on);
fan_on_bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.fan_on);
ac_on_bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ac_on);
}
@Override
public void onDraw(Canvas canvas) {
int CENTER_X = canvas.getWidth() / 2 - 24;
int CENTER_Y = canvas.getHeight() / 2 - zed_bitMap.getHeight();
int FULL_WIDTH = canvas.getWidth();
int FULL_HEIGHT = canvas.getHeight();
canvas.drawBitmap(zed_bitMap, CENTER_X, CENTER_Y, paint);
paint.setStrokeWidth(3);
paint.setPathEffect(new DashPathEffect(new float[] { 5, 5, 5, 5 }, 0));
canvas.drawBitmap(ac_on_bitmap, START_X, START_Y, paint);
canvas.drawLine(START_X + 48, START_Y + 48, CENTER_X + 10,
CENTER_Y + 10, paint);
canvas.drawLine(250, START_Y + 48, CENTER_X + 10, CENTER_Y + 10, paint);
canvas.drawBitmap(lamp_on_bitmap, 250 - 24, START_Y, paint);
canvas.drawLine(500, START_Y + 48, CENTER_X + 10, CENTER_Y + 10, paint);
canvas.drawBitmap(fan_on_bitmap, x,y, paint);
// canvas.drawLine(FULL_WIDTH-40,FULL_HEIGHT-120,CENTER_X+30,CENTER_Y+30,
// paint);
// canvas.drawBitmap(light_off_bitmap, FULL_WIDTH-60-24,FULL_HEIGHT-130,
// paint);
}
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
}
break;
case MotionEvent.ACTION_MOVE: {
x = (int) event.getX();
y = (int) event.getY();
invalidate();
}
break;
case MotionEvent.ACTION_UP:
x = (int) event.getX();
y = (int) event.getY();
Log.d(APPConstant.LOG_TAG, ".................." + x + "......" + y);
Toast.makeText(getContext(), ".................." + x + "......" + y,Toast.LENGTH_SHORT).show();
invalidate();
break;
}
return true;
}
}
解决方案
public class SimpleDrag extends View {
private final int INVALID_INDEX = -1;
private final int mTotalItems = 5;
private ArrayList<Rect> mItemsCollection;
private ArrayList<Point> mActiveDragPoints;
private ArrayList<Rect> mActiveRects;
private Paint mPaint;
/**
* @param context
* @return of type SimpleDrag
* Constructor function
* @since Feb 19, 2013
* @author rajeshcp
*/
public SimpleDrag(Context context) {
super(context);
init();
}
/**
* @param context
* @param attrs
* @return of type SimpleDrag
* Constructor function
* @since Feb 19, 2013
* @author rajeshcp
*/
public SimpleDrag(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
/**
* @param context
* @param attrs
* @param defStyle
* @return of type SimpleDrag
* Constructor function
* @since Feb 19, 2013
* @author rajeshcp
*/
public SimpleDrag(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
/* (non-Javadoc)
* @see android.view.View#onDraw(android.graphics.Canvas)
* @since Feb 19, 2013
* @author rajeshcp
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.BLUE, PorterDuff.Mode.CLEAR);
for( Rect rect : mItemsCollection)
{
canvas.drawRect(rect, mPaint);
}
}
/**
* @param of type null
* @return of type null
* function which will initialize the view
* @since Feb 20, 2013
* @author rajeshcp
*/
private void init()
{
mActiveRects = new ArrayList<Rect>(mTotalItems);
mActiveDragPoints = new ArrayList<Point>(mTotalItems);
mItemsCollection = new ArrayList<Rect>();
for( int i = 0; i < mTotalItems; i++)
{
Rect rect = new Rect(i * 100, i * 100, (i + 1) * 100, (i + 1) * 100);
mItemsCollection.add(rect);
}
mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.RED);
}
/* (non-Javadoc)
* @see android.view.View#onTouchEvent(android.view.MotionEvent)
* @since Feb 19, 2013
* @author rajeshcp
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getActionMasked();
final int pointer = event.getActionIndex();
switch (action) {
case MotionEvent.ACTION_DOWN :
Point touchDown = new Point((int)event.getX(), (int)event.getY());
lookForIntersection(touchDown);
break;
case MotionEvent.ACTION_UP :
case MotionEvent.ACTION_CANCEL :
mActiveDragPoints.removeAll(mActiveDragPoints);
mActiveRects.removeAll(mActiveRects);
break;
case MotionEvent.ACTION_MOVE :
int count = 0;
for(Rect rect : mActiveRects)
{
Point curretPoint = new Point((int)event.getX(count), (int)event.getY(count));
moveRect(curretPoint, mActiveDragPoints.get(count), rect);
count++;
}
Log.d(getClass().getName(), "Active Rects" + mActiveRects.size());
Log.d(getClass().getName(), "Active Points" + mActiveDragPoints.size());
invalidate();
break;
case MotionEvent.ACTION_POINTER_DOWN :
touchDown = new Point((int)event.getX(pointer), (int)event.getY(pointer));
lookForIntersection(touchDown);
//Log.d(getClass().getName(), "ACTION_POINTER_DOWN" + pointer);
break;
case MotionEvent.ACTION_POINTER_UP :
int index = getIntersectionRectIndex(new Point((int)event.getX(pointer), (int)event.getY(pointer)));
if( index != INVALID_INDEX )
{
Rect rect = mItemsCollection.get(index);
mActiveDragPoints.remove(mActiveRects.indexOf(rect));
mActiveRects.remove(rect);
}
break;
default:
break;
}
return true;
}
/**
* @param touchDown of type Point
* @return of type null
* function which will find the
* intersecting rect and add to the
* active collection
* @since Feb 20, 2013
* @author rajeshcp
*/
private void lookForIntersection(Point touchDown)
{
final int index = getIntersectionRectIndex(touchDown);
if( index != INVALID_INDEX )
{
final Rect rect = mItemsCollection.get(index);
if( mActiveRects.indexOf(rect) == INVALID_INDEX )
{
mActiveDragPoints.add(touchDown);
mActiveRects.add(mItemsCollection.get(index));
}
}
Log.d(getClass().getName(), "Active Rects" + mActiveRects.size());
Log.d(getClass().getName(), "Active Points" + mActiveDragPoints.size());
}
/**
* @param point of type Point
* @return of type int
* function which will return the index of
* the rect contaning the given point
* @since Feb 20, 2013
* @author rajeshcp
*/
private int getIntersectionRectIndex(final Point point)
{
int index = INVALID_INDEX;
for(Rect rect : mItemsCollection)
{
if( rect.contains(point.x, point.y) )
{
index = mItemsCollection.indexOf(rect);
break;
}
}
return index;
}
/**
* @param currentPoint of type Point
* @param prevPoint of type Point
* @param rect of type Rect
* @return of type null
* function which will move the change the
* bounds of teh rect
* @since Feb 20, 2013
* @author rajeshcp
*/
private void moveRect(Point currentPoint, Point prevPoint, final Rect rect)
{
int xMoved = currentPoint.x - prevPoint.x;
int yMoved = currentPoint.y - prevPoint.y;
rect.set(rect.left + xMoved, rect.top + yMoved, rect.right + xMoved, rect.bottom + yMoved);
mActiveDragPoints.set(mActiveDragPoints.indexOf(prevPoint), currentPoint);
}
}
This allow you to select more than one bitmap at a time as well. This case I'm only using rectangles you can fill this rectangles with bitmaps.
这篇关于如何移动单帆布的android多个位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文