80,352
社区成员
发帖
与我相关
我的任务
分享
package cc.pic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MyView extends View {
public void setBmp1(Bitmap bmp1) {
this.bmp1 = bmp1;
mBitmap=bmp1;
}
Canvas mCanvas;
Bitmap mBitmap, bmp1, bmp2;
Paint mPaint;
int flag = 0;
float movex, movey, lastx, lasty, initx, inity, initx2, inity2;
float X_r, Y_r;
public MyView(Context context) {
super(context);
X_r = 0;
Y_r = 0;
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
X_r = 0;
Y_r = 0;
init();
}
public void init() {
if(mBitmap != null)
{
mBitmap =Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), Config.ARGB_8888);
mCanvas=new Canvas(mBitmap);
mPaint=new Paint();
mPaint.setColor(0xFFFFFFFF);
// drawImage(bmp2, 50, 0);
drawImage(bmp1, 150, 150);
}
else
{
mBitmap=null;
}
Toast.makeText(getContext(), "init ing ", 1).show();
}
protected void onDraw(Canvas canvas) {
if(mBitmap!=null)
{
canvas.drawColor(0xa1a1a1);
canvas.drawBitmap(mBitmap, X_r, Y_r, mPaint);
Toast.makeText(getContext(), "ondraw "+flag, 1).show();
}
flag++;
super.onDraw(canvas);
}
List<HashMap> list = new ArrayList<HashMap>();
HashMap map = null;
/* 在给定的位置画位图记录好他们的初始化坐标,放入一个集合中 */
public void drawImage(Bitmap bmp, float x, float y) {
if (bmp == null)
return;
/* 记录好图片坐标 */
map = new HashMap();
map.put("x", x);
map.put("y", y);
map.put("bmp", bmp);
list.add(map);
Log.d("MyView", "drawImage--->" + initx + ":" + inity);
float x_1 = bmp1.getWidth() / 2;
float y_1 = bmp1.getHeight() / 2;
mCanvas.drawBitmap(bmp, x - x_1, y - y_1, mPaint);
Toast.makeText(getContext(), "drawImage ", 1).show();
invalidate();
}
/* 传入点击的坐标点获得位图 */
public Bitmap bmpClick(float mx, float my) {
Bitmap bmpy = null;
HashMap hashMap = null;
for (Iterator<HashMap> it = list.iterator(); it.hasNext();) {
hashMap = it.next();
float dx = Float.parseFloat(hashMap.get("x").toString());// 获取到它的初始化X坐标
float dy = Float.parseFloat(hashMap.get("y").toString());// 获取到它的初始化Y坐标
bmpy = (Bitmap) hashMap.get("bmp");// 获取到它的初始化的位图
Log.i("MyView", "bmpClick---> " + dx + ":" + dy);
if (mx >= (dx - bmpy.getWidth() / 2)
&& mx <= (dx + bmpy.getWidth() / 2)
&& my >= (dy - bmpy.getHeight() / 2)
&& my <= (dy + bmpy.getHeight() / 2)) {
return bmpy;
}
}
return null;
}
public float getX_r() {
return X_r;
}
public float getY_r() {
return Y_r;
}
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
// 点击时的(x,y),记录初始位置
case MotionEvent.ACTION_DOWN:
mDragBitmap = bmpClick(x, y);
break;
// 移动时更新(x,y),同时更新图片
case MotionEvent.ACTION_MOVE:
if (mDragBitmap != null) {
if (mDragBitmap.equals(bmp1)) {
movex = bmp1.getWidth() / 2;
movey = bmp1.getHeight() / 2;
mBitmap = bmp1.copy(Bitmap.Config.ARGB_8888, true);
X_r = x - movex;
Y_r = y - movey;
invalidate();
}
/*
* else{ movex = bmp2.getWidth()/2; movey = bmp2.getHeight()/2;
* mBitmap = bmp2.copy(Bitmap.Config.ARGB_8888, true); X_r =
* x-movex; Y_r = y-movey; invalidate(); }
*/
}
break;
// 弹起时更新(x,y),并记录位置
case MotionEvent.ACTION_UP:
if (mDragBitmap != null) {
map = new HashMap();
map.put("x", x);
map.put("y", y);
map.put("bmp", mDragBitmap);
list.add(map);
Log.e("bitmap list", "insert finish : [" + x + "," + y + "]");
}
mDragBitmap = null;
break;
}
return true;
}
private Bitmap mDragBitmap = null;
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#666666">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/a"
android:padding="20dp"
android:id="@+id/wordImageView" />
<cc.test.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:id="@+id/myview1"></cc.test.MyView>
<Gallery
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/wordGalleryView" />
</Relati
package cc.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class cropPoc extends Activity{
private ImageView wordView;
private MyView myView = null;
private Gallery gallery;
Integer[] word={
R.drawable.funny1,
R.drawable.rotate,
R.drawable.tab_b,
};
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery=(Gallery) this.findViewById(R.id.wordGalleryView);
wordView = (ImageView) this.findViewById(R.id.wordImageView);
myView=(MyView)this.findViewById(R.id.myview1);
gallery.setAdapter(new ImageAdapterss(getApplicationContext(), word));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Bitmap b;
switch(position)
{
case 0:
b=BitmapFactory.decodeResource(getResources(), R.drawable.funny1);
myView.setBmp1(b);
myView.init();
myView.requestFocus();
myView.invalidate();
break;
case 1:
b=BitmapFactory.decodeResource(getResources(), R.drawable.rotate);
myView.setBmp1(b);
myView.init();
myView.requestFocus();
myView.invalidate();
break;
case 2:
b=BitmapFactory.decodeResource(getResources(), R.drawable.tab_b);
myView.setBmp1(b);
myView.init();
myView.requestFocus();
myView.invalidate();
break;
}
}
});
}
}
class ImageAdapterss extends BaseAdapter {
private Context mContext;
private Integer[] mImageIds;
public ImageAdapterss(Context mContext, Integer[] mImageIds) {
this.mContext = mContext;
this.mImageIds = mImageIds;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageview = new ImageView(mContext);
// 设置资源
imageview.setImageResource(mImageIds[position]);
// 设置布局图片的分辨率
imageview.setLayoutParams(new Gallery.LayoutParams(80, 80));
// 设置显示的类型
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageview;
}
}