图片上涂鸦
在android设备上对照片进行涂鸦,要解决的不同手机屏幕大小不一致的问题,怎样做才能去适应不同的手机屏幕,这里,可以用到一个相似的数学知识。
就是,获取手机屏幕的像素值,再得到图片大小的像素值,计算出两个值的比值,然后监听触摸事件,监听触摸事件得到的数据,处理一下,就可对应到图片具体的像素点。看一下效果。
好了,看一下代码
public class ScrawlActivity extends Activity {
ImageView imageView;
double pictureRelativeLeft, pictureRelativeTop, pictureRelativeRight,
pictureRelativeButtom;
double imageViewLeft, imageViewTop, imageViewRight, imageViewButtom;
double pictureRealLeft, pictureRealTop, pictureRealRight,
pictureRealButtom;
Bitmap bitmap;
double proportionWidth, proportionHeight;
double bitmapWidth, bitmapHeight;
Canvas canvas;
Path path;
double preX, preY;
Paint paint;
boolean hasOut=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InitToolBar.initToolBar(this);
setContentView(R.layout.activity_scrawl);
setActionBar();
imageView = (ImageView) findViewById(R.id.scrawlImageView);
BitmapFactory.Options bfoOptions = new BitmapFactory.Options();
bfoOptions.inScaled = false;
Intent intent=getIntent();
bitmap=((APP)getApplication()).bitmap.copy(Bitmap.Config.ARGB_8888, true);
imageView.setImageBitmap(bitmap);
bitmapWidth = bitmap.getWidth();
bitmapHeight = bitmap.getHeight();
canvas = new Canvas();
System.out.println(bitmap);
canvas.setBitmap(bitmap);
setPiont();
path = new Path();
System.out.println("bitmap: " + bitmapWidth + " " + bitmapHeight);
}
void setActionBar(){
ActionBar actionBar=getActionBar();
actionBar.setTitle(" ");
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setBackgroundDrawable(getResources().getDrawable(
R.drawable.actionbar));
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus == true) {
Matrix matrix = imageView.getImageMatrix();
Rect rect = imageView.getDrawable().getBounds();
float[] values = new float[9];
matrix.getValues(values);
pictureRelativeLeft = values[2];
pictureRelativeTop = values[5];
pictureRelativeRight = pictureRelativeLeft + rect.width()
* values[0];
pictureRelativeButtom = pictureRelativeTop + rect.height()
* values[0];
int[] location = new int[2];
imageView.getLocationOnScreen(location);
imageViewLeft = location[0];
imageViewTop = location[1];
System.out.println("imageView:" + imageViewLeft + " "
+ imageViewTop);
imageViewRight = imageView.getRight();
imageViewButtom = imageView.getBottom();
setPictureRealPosition();
proportionWidth = bitmapWidth
/ (pictureRealRight - pictureRealLeft);
proportionHeight = bitmapHeight
/ (pictureRealButtom - pictureRealTop);
}
}
void setPiont() {
paint = new Paint(Paint.DITHER_FLAG);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setAntiAlias(true);
paint.setDither(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
double x = event.getX();
double y = event.getY();
if (x >= pictureRealLeft && x <= pictureRealRight
&& y >= pictureRealTop && y <= pictureRealButtom) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = (x - pictureRealLeft) * proportionWidth;
y = (y - pictureRealTop) * proportionHeight;
path.moveTo((float) x, (float) y);
preX = x;
preY = y;
break;
case MotionEvent.ACTION_MOVE:
System.out.println(x + " " + y);
x = (x - pictureRealLeft) * proportionWidth;
y = (y - pictureRealTop) * proportionHeight;
if(hasOut==true){
path.reset();
path.moveTo((float) x, (float) y);
preX=x;
preY=y;
System.out.println("reset");
hasOut=false;
}
path.quadTo((float) preX, (float) preY, (float) x, (float) y);
preX = x;
preY = y;
break;
case MotionEvent.ACTION_UP:
System.out.println(x + " " + y);
x = (x - pictureRealLeft) * proportionWidth;
y = (y - pictureRealTop) * proportionHeight;
canvas.drawPath(path, paint);
path.reset();
break;
}
} else {
path.reset();
hasOut=true;
}
canvas.drawPath(path, paint);
imageView.setImageBitmap(bitmap);
return false;
}
void setPictureRealPosition() {
pictureRealLeft = imageViewLeft + pictureRelativeLeft;
pictureRealTop = imageViewTop + pictureRelativeTop;
pictureRealRight = imageViewLeft + pictureRelativeRight;
pictureRealButtom = imageViewTop + pictureRelativeButtom;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.scrawl, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.scrawlSure) {
Intent intent =new Intent();
intent.putExtra("result", true);
setResult(1, intent);
((APP)getApplication()).bitmap=bitmap;
finish();
return true;
}
if(id==R.id.scrawlCancel){
setResult(2);
finish();
return true;
}
if(id==android.R.id.home){
setResult(2);
finish();
return true;
}
return false;
}
@Override
protected void onDestroy() {
super.onDestroy();
setResult(2);
}
}