本版等级:
本版专家分:40
结帖率:100%得分:0 回复于: 2012-11-16 12:17:35
package com.zhutieju.openphoto;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
public class OpenPhotoActivity extends Activity {
Button previousBtn,nextBtn;
ImageView picture;
Cursor cursor;
String imageFilePath;
int fileColumn;
Bitmap pictureBmp,cloth;
Canvas canvas;
Paint paint;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open_photo);
//获取各个控件对象
previousBtn = (Button)findViewById(R.id.previous);
nextBtn = (Button)findViewById(R.id.next);
picture = (ImageView)findViewById(R.id.picture);
//获取cursor
String[] columns = {Media.DATA,Media._ID,Media.TITLE,Media.DISPLAY_NAME};
cursor = managedQuery(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);
//通过cursor获取各个列的索引
fileColumn = cursor.getColumnIndexOrThrow(Media.DATA);
//ImageView触屏事件
picture.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float downX = 0;
float downY = 0;
float upX = 0;
float upY = 0;
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
pictureBmp = Bitmap.createBitmap(getBitmap(imageFilePath), (int)downX, (int)downY, 20, 20);
canvas.drawBitmap(pictureBmp, event.getX(),event.getY() , paint);
picture.setImageBitmap(cloth);
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
upX = event.getX();
upY = event.getY();
break;
default:
break;
}
return true;
}
});
//为previousBtn添加点击事件
previousBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(cursor.moveToNext()) {
imageFilePath = cursor.getString(fileColumn);
//获取幕布
pictureBmp = getClothBmp();
//显示幕布或者图片
picture.setImageBitmap(pictureBmp);
} else {
cursor.moveToFirst();
//获取幕布
pictureBmp = getClothBmp();
//显示幕布
picture.setImageBitmap(pictureBmp);
}
}
});
//为nextBtn添加点击事件
nextBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/* if(cursor.moveToPrevious()) {
imageFilePath = cursor.getString(fileColumn);
pictureBmp = getBitmap(imageFilePath);
picture.setImageBitmap(pictureBmp);
//获取幕布
clothBmp = getClothBmp();
//显示幕布
cloth.setImageBitmap(clothBmp);
} else {
cursor.moveToLast();
imageFilePath = cursor.getString(fileColumn);
pictureBmp = getBitmap(imageFilePath);
picture.setImageBitmap(pictureBmp);
//获取幕布
clothBmp = getClothBmp();
//显示幕布
cloth.setImageBitmap(clothBmp);
}*/
//drawTransparent(clothBmp);
}
});
}
@Override
protected void onResume() {
paint = new Paint();
// TODO Auto-generated method stub
super.onResume();
//查询并显示图像
if(cursor.moveToFirst()) {
imageFilePath = cursor.getString(fileColumn);
pictureBmp = getBitmap(imageFilePath);
//获取幕布
pictureBmp = getClothBmp();
//显示幕布
picture.setImageBitmap(pictureBmp);
}
}
/**
* 通过路径返回以屏幕宽高比例的位图
* @param imageFilePath
* @return
*/
private Bitmap getBitmap(String imageFilePath) {
Bitmap bmp;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bmp = BitmapFactory.decodeFile(imageFilePath, options);
int width = options.outWidth / getWindowManager().getDefaultDisplay().getWidth();
int height = options.outHeight / getWindowManager().getDefaultDisplay().getHeight();
options.inSampleSize = width>height?width:height;
options.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(imageFilePath, options);
return bmp;
}
/**
* 获取一个所需要的幕布
* @return
*/
private Bitmap getClothBmp() {
cloth = Bitmap.createBitmap(pictureBmp.getWidth(),pictureBmp.getHeight(),pictureBmp.getConfig());
canvas = new Canvas(cloth);
canvas.drawColor(Color.GRAY);
//canvas.drawBitmap(clothBmp1, 10, 10,null);
return cloth;
}
}