package com.test;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MoveDemoActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new Draw(this));
}
// the Canvas to Draw
class Draw extends View
{
Bmp bmp[];
// construct
public Draw(Context context)
{
super(context);
bmp = new Bmp[4];
{
for(int i = 0; i < 4; i++)
{
bmp[i] = new Bmp(BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.test)), i, i * 50f, i * 60f);
}
}
this.pic = bmp;
}
// construct
public Draw(Context context, Bmp[] pic)
{
this(context);
this.pic = pic;
}
@Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawBitmap(canvasBitmap, 0, 0, null);
}
// OntouchEvent
@Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
this.DownX = event.getX();
this.DownY = event.getY();
}
// find the picture that User touch and reOrder
// this.X = event.getX();
// this.Y = event.getY();
// order(event);
// draw the Canvas
if(event.getAction() == MotionEvent.ACTION_MOVE)
{
invalidate();
this.X = event.getX();
this.Y = event.getY();
order(event);
this.canvas.drawColor(-232432445);
for(int i = 0; i < 3; i++)
{
tempBitmap = pic[0].findByPiority(pic, i);
this.canvas.drawBitmap(tempBitmap.getPic(), tempBitmap.getXY(1) - tempBitmap.getPic().getWidth() / 2,
tempBitmap.getXY(2) - tempBitmap.getPic().getHeight() / 2, null);
}
tempBitmap = pic[0].findByPiority(pic, 3);
// if((Math.abs(pic[0].findByPiority(pic, 3).getXY(1) - this.X) < pic[0].findByPiority(pic, 3).getPic().getWidth() / 2)
// && (Math.abs(pic[0].findByPiority(pic, 3).getXY(2) - this.Y) < pic[0].findByPiority(pic, 3).getPic().getHeight() / 2))
// {
this.canvas.drawBitmap(tempBitmap.getPic(), pic[0].findByPiority(pic, 3).preX - tempBitmap.getPic().getWidth() / 2,
pic[0].findByPiority(pic, 3).preY - tempBitmap.getPic().getWidth() / 2, null);
pic[0].findByPiority(pic, 3).preX = X;
pic[0].findByPiority(pic, 3).preY = Y;
// }
// else
// {
// this.canvas.drawBitmap(tempBitmap.getPic(), tempBitmap.getXY(1) - tempBitmap.getPic().getWidth() / 2,
// tempBitmap.getXY(2) - tempBitmap.getPic().getWidth() / 2, null);
// }
if(event.getAction() == MotionEvent.ACTION_UP)
{
pic[0].findByPiority(pic, 3).preX = X;
pic[0].findByPiority(pic, 3).preY = Y;
}
}
return true;
}
// reorder the picture
public void order(MotionEvent event)
{
for(int i = 3; i > -1; i--)
{
if((Math.abs(pic[0].findByPiority(pic, i).getXY(1) - this.X) < pic[0].findByPiority(pic, i).getPic().getWidth() / 2)
&& (Math.abs(pic[0].findByPiority(pic, i).getXY(2) - this.Y) < pic[0].findByPiority(pic, i).getPic().getHeight() / 2))
{
for(Bmp bmp: pic)
{
if(bmp.getPriority() > pic[0].findByPiority(pic, i).getPriority())
{
bmp.priority--;
}
}
pic[0].findByPiority(pic, i).setPiority(3);
Log.i("current pic", pic[0].findByPiority(pic, i).toString());
Log.i("current pic******", pic[0].findByPiority(pic, 3).toString());
// pic[0].findByPiority(pic, i).preX = X;
// pic[0].findByPiority(pic, i).preY = Y;
Log.i("dsfdsf", Integer.toString(i));
return;
}
}
}
private Bitmap canvasBitmap = Bitmap.createBitmap(480, 500, Config.ARGB_8888);
private Bmp tempBitmap = null;
private Canvas canvas = new Canvas(canvasBitmap);
private float X = 0f;
private float Y = 0f;
private float DownX = 0f;
private float DownY = 0f;
private Bmp[] pic = new Bmp[4];
}
// @param pic:the Bitmap to draw
// @param piority: the order to draw picture
// @param preX,preY: the X and Y
class Bmp
{
// 构造器
public Bmp(Bitmap pic, int piority)
{
this.pic = pic;
this.priority = piority;
}
// 构造器
public Bmp(Bitmap pic, int priority, float preX, float preY)
{
this(pic, priority);
this.preX = preX + pic.getWidth() / 2 * 1.5f;
this.preY = preY + pic.getHeight() / 2 * 1.5f;
}
// findPiority
public Bmp findByPiority(Bmp[] pic, int priority)
{
for(Bmp p : pic)
{
if(p.priority == priority)
{
return p;
}
}
return null;
}
// set Priority
public void setPiority(int priority)
{
this.priority = priority;
}
// return Priority
public int getPriority()
{
return this.priority;
}
// return X and Y
public float getXY(int i)
{
if(i == 1)
{
return this.preX;
}
else if(i == 2)
{
return this.preY;
}
return (Float) null;
}
// getPicture
public Bitmap getPic()
{
return this.pic;
}
float preX = 0;
float preY = 0;
Bitmap pic = null;
int priority = 0;
}
}
package com.test; import android.R.integer; import android.app.Activity; import android.content.Co
最新推荐文章于 2024-01-25 10:59:15 发布