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.Matrix;
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(Bitmap.createScaledBitmap(BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.you)), 240, 240, false), i, i * 50f, i * 60f);
}
}
this.pic = bmp;
this.canvas.drawColor(-232432445);
for(int i = 0; i < 4; 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);
}
}
// 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);
}
// Oc Click
// OntouchEvent
@Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
// this.DownX = event.getX() - pic[3].findByPiority(pic, 3).preX;
// this.DownY = event.getY() - pic[3].findByPiority(pic, 3).preY;
order(event);
this.X = event.getX();
this.Y = event.getY();
CX = pic[3].findByPiority(pic, 3).getXY(1) - event.getX();
CY = pic[3].findByPiority(pic, 3).getXY(2) - event.getY();
Begin = true;
if(event.getPointerCount() > 1)
{
X_1 = event.getX(0);
X_2 = event.getX(1);
Y_1 = event.getY(0);
Y_2 = event.getY(1);
Log.i("duo dian", String.valueOf(event.getPointerCount()));
}
// Log.i("down", String.valueOf(Begin));
}
// find the picture that User touch and reOrder
// draw the Canvas
if(event.getAction() == MotionEvent.ACTION_MOVE && Begin && event.getPointerCount() == 1)
{
this.X = event.getX();
this.Y = event.getY();
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.DownX < pic[0].findByPiority(pic, 3).getPic().getWidth() / 2
// && this.DownY < pic[0].findByPiority(pic, 3).getPic().getHeight() / 2
// && event.getPointerCount() == 1
)
{
this.canvas.drawBitmap(tempBitmap.getPic(), X - tempBitmap.getPic().getWidth() / 2 + CX,
Y - tempBitmap.getPic().getHeight() / 2 + CY, null);
// pic[0].findByPiority(pic, 3).preX = X + CX[3];
// pic[0].findByPiority(pic, 3).preY = Y + CY[3];
tempBitmap.preX = X + CX;
tempBitmap.preY = Y + CY;
// Log.i(tempBitmap.toString(), Float.toString(tempBitmap.getXY(1)) + " " + Float.toString(tempBitmap.getXY(2)));
}
else
{
this.canvas.drawBitmap(tempBitmap.getPic(), tempBitmap.getXY(1) - tempBitmap.getPic().getWidth() / 2,
tempBitmap.getXY(2) - tempBitmap.getPic().getWidth() / 2, null);
}
}
// 多点触控
if (event.getPointerCount() >= 2)
{
X_1 = event.getX(0);
X_2 = event.getX(1);
Y_1 = event.getY(0);
Y_2 = event.getY(1);
Log.i("2 touch ", String.valueOf(event.getPointerCount()));
tan = (Y_2 - Y_1) / (X_2 - X_1);
rotary = (float) Math.atan((double)tan);
tempBitmap.matrix.setRotate(rotary);
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_1) < pic[0].findByPiority(pic, 3).getPic().getWidth() / 2)
&& (Math.abs(pic[0].findByPiority(pic, 3).getXY(2) - this.Y_1) < pic[0].findByPiority(pic, 3).getPic().getHeight() / 2)
&&(Math.abs(pic[0].findByPiority(pic, 3).getXY(1) - this.X_2) < pic[0].findByPiority(pic, 3).getPic().getWidth() / 2)
&& (Math.abs(pic[0].findByPiority(pic, 3).getXY(2) - this.Y_2) < pic[0].findByPiority(pic, 3).getPic().getHeight() / 2)
)
{
this.canvas.drawBitmap(tempBitmap.getPic(), X - tempBitmap.getPic().getWidth() / 2 + CX,
Y - tempBitmap.getPic().getHeight() / 2 + CY, null);
tempBitmap.preX = X + CX;
tempBitmap.preY = Y + CY;
}
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)
{
CX = 0f;
CY = 0f;
Begin = false;
// Log.i("Begin == flase", String.valueOf(Begin));
}
invalidate();
return true;
}
// reorder the picture
public void order(MotionEvent event)
{
Bmp temp = null;
for(int i = 3; i > -1; i--)
{
if(
(Math.abs(pic[0].findByPiority(pic, i).getXY(1) - event.getX()) < pic[0].findByPiority(pic, i).getPic().getWidth() / 2)
&& (Math.abs(pic[0].findByPiority(pic, i).getXY(2) - event.getY()) < pic[0].findByPiority(pic, i).getPic().getHeight() / 2)
// && DownX < pic[0].findByPiority(pic, i).getPic().getWidth() / 2
// && DownY < pic[0].findByPiority(pic, i).getPic().getHeight() / 2
)
{
temp = pic[0].findByPiority(pic, i);
for(Bmp bmp: pic)
{
if(bmp.getPriority() > pic[0].findByPiority(pic, i).getPriority())
{
bmp.priority--;
}
}
temp.setPiority(3);
Begin = true;
// Log.i("the Order ", temp.toString());
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;
@SuppressWarnings("unused")
private float DownX = 0f;
@SuppressWarnings("unused")
private float DownY = 0f;
private Bmp[] pic = new Bmp[4];
private float X_1;
private float X_2;
private float Y_1;
private float Y_2;
private float tan;
private float rotary;
private float CX = 0f;
private float CY = 0f;
private boolean Begin = true;
}
// @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;
float width = 0;
float height = 0;
Bitmap pic = null;
int priority = 0;
private Matrix matrix = new Matrix();
}
}
androi 图片自由拼接
最新推荐文章于 2024-08-30 15:32:38 发布