闲来无事,今天写了一下怎么裁剪图片,写的很烂,放在这里以后看看自己有没有进步.............
首先给个效果图了...........
1,拖动矩形框,红色矩形框以外是一个半透明的罩,通过画出四个半透明的矩形,可以实现效果
2,拖动红框及拉伸红框获取坐标,拉伸只能通过红框的上面那个红线点击向外移动放大,向左里移动缩小,哈哈,还有很多地方没有做,不想做了,懒啊,有想法的时候再做吧.......
3,点击红框以外的地方时候,会保存裁剪图片,最后点击save去掉自定义的view,把裁剪的图片放在imageview里面。(底层有个图片是本身imageview有的哦)
代码实现:
自定义view:
package com.zte.caijian;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class myView extends View {
private int l = 20;
private int t = 20;
private int r = 100;
private int b = 100;
Bitmap bitmap;
Bitmap bitmapOld;
@SuppressLint({ "NewApi", "NewApi" })
public myView(Context context, Bitmap bitmap) {
super(context);
this.bitmap = Bitmap.createScaledBitmap(bitmap, 480, 540, true);
// setBackgroundColor(Color.BLACK);
// setAlpha(0.8f);
}
int xy[] = new int[2];
int temp[] = new int[2];
int tempM[] = new int[2];
boolean mFlag = false;
boolean mFlagM = false;
boolean mFlag1 = false;
boolean mFlag2 = false;
boolean kaishi = true;
int change = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
xy[0] = (int) event.getX();
xy[1] = (int) event.getY();
if (xy[0] < r && xy[0] > l && xy[1] < b && xy[1] > t) {
mFlag2 = false;
mFlag = true;
mFlag1 = false;
} else {
mFlag = false;
}
if (xy[0] < r && xy[0] > l && xy[1] == t) {
mFlag1 = true;
} else {
if (tmL == 0 || tmR == 0 || tmT == 0) {
} else {
if (bitmapOld == null) {
Log.i("RG", "bitmap--->>>h" + bitmap.getHeight());
Log.i("RG", "bitmap--->>>W" + bitmap.getWidth());
Log.i("RG", "tmL--->>>W" + tmL);
Log.i("RG", "tmT--->>>W" + tmT);
Log.i("RG", "tmR--->>>W" + tmR);
Log.i("RG", "tmB--->>>W" + tmB);
bitmapOld = Bitmap.createBitmap(bitmap, tmL, tmT, tmR
- tmL, tmB - tmT, null, true);
} else {
bitmapOld = null;
bitmapOld = Bitmap.createBitmap(bitmap, tmL, tmT, tmR
- tmL, tmB - tmT, null, true);
}
}
}
case MotionEvent.ACTION_MOVE:
temp[0] = (int) event.getX();
temp[1] = (int) event.getY();
if (mFlag) {
if (xy[0] == temp[0] && xy[1] == temp[1]) {
} else {
mFlagM = true;
l = temp[0];
t = temp[1];
if (tuodongH > 0) {
r = l + tuodongW;
b = t + tuodongH;
} else {
r = l + 80;
b = t + 80;
}
invalidate();
}
}
if (mFlag1) {
Log.i("RG", "panduanl you wentil ---->>>" + xy[0] + ",,,,"
+ xy[1]);
if (temp[0] > xy[0] || temp[1] < xy[1]) {
Log.i("RG", "-------mFlag1------>>>>>>" + temp[0] + ",,,,"
+ temp[1]);
mFlag2 = true;
change += 5;
// l = xy[0];
// t = xy[1];
tempM[0] = r = (l + 80) + change;
tempM[1] = b = (t + 80) + change;
invalidate();
} else {
change -= 5;
tempM[0] = r = (l + 80) + change;
tempM[1] = b = (t + 80) + change;
invalidate();
}
}
}
return true;
}
int tuodongH;
int tuodongW;
int tmL;
int tmT;
int tmR;
int tmB;
public Bitmap getBitmap() {
return bitmapOld;
}
public void Pcanvas(Canvas canvas) {
Paint mPaint = new Paint();
mPaint.setStyle(Style.STROKE);
mPaint.setStrokeMiter(6);
mPaint.setStrokeWidth(3);
mPaint.setColor(Color.RED);
canvas.drawRect(l, t, r, b, mPaint);
tuodongH = b - t;
tuodongW = r - l;
Paint mPaint1 = new Paint();
mPaint1.setColor(Color.BLACK);
mPaint1.setAlpha(100);
canvas.drawRect(0, 0, 480, t, mPaint1);// 1111
canvas.drawRect(0, b, 480, 800, mPaint1);
canvas.drawRect(0, t, l, b, mPaint1);
canvas.drawRect(r, t, 480, b, mPaint1);
}
@Override
protected void onDraw(Canvas canvas) {
if (mFlag1 && mFlag2) {
Pcanvas(canvas);
// mFlag1 = false;
// mFlag2 = false;
return;
}
if (mFlag == true && mFlagM == true) {
Paint mPaint = new Paint();
mPaint.setStyle(Style.STROKE);
mPaint.setStrokeMiter(6);
mPaint.setStrokeWidth(3);
mPaint.setColor(Color.RED);
canvas.drawRect(l, t, r, b, mPaint);
tmL = l;
tmT = t;
tmR = r;
tmB = b;
Paint mPaint1 = new Paint();
mPaint1.setColor(Color.BLACK);
mPaint1.setAlpha(100);
canvas.drawRect(0, 0, 480, t, mPaint1);// 1111
canvas.drawRect(0, b, 480, 800, mPaint1);
canvas.drawRect(0, t, l, b, mPaint1);
canvas.drawRect(r, t, 480, b, mPaint1);
return;
}
if (kaishi) {
Paint mPaint = new Paint();
mPaint.setStyle(Style.STROKE);
mPaint.setStrokeMiter(6);
mPaint.setStrokeWidth(3);
mPaint.setColor(Color.RED);
canvas.drawRect(l, t, r, b, mPaint);
Paint mPaint1 = new Paint();
mPaint1.setColor(Color.BLACK);
mPaint1.setAlpha(100);
canvas.drawRect(0, 0, 480, t, mPaint1);// 1111
canvas.drawRect(0, b, 480, 800, mPaint1);
canvas.drawRect(0, t, l, b, mPaint1);
canvas.drawRect(r, t, 480, b, mPaint1);
kaishi = false;
} else {
return;
}
super.onDraw(canvas);
}
}
package com.zte.caijian;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity implements OnClickListener {
Button save;
Button cencal;
ImageView mImageView;
ImageView mCaijian;
myView my;
Bitmap bitmap;
LayoutParams lp = new LayoutParams(480, 600);
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aa);
my = new myView(MainActivity.this, bitmap);
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.addView(my);
layout.setTop(50);
this.addContentView(layout, lp);
}
private void init() {
save = (Button) findViewById(R.id.save);
cencal = (Button) findViewById(R.id.cencal);
mImageView = (ImageView) findViewById(R.id.imageview);
mCaijian = (ImageView) findViewById(R.id.caijian);
save.setOnClickListener(this);
cencal.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.save:
Log.i("RG", "CLICK-----");
Bitmap bitmap = my.getBitmap();
mImageView.setImageBitmap(bitmap);
break;
case R.id.cencal:
MainActivity.this.finish();
break;
}
}
}