Android custom view allows you to crop images without using the System

Source: Internet
Author: User

I have nothing to worry about. Today I wrote about how to crop the image, which is very bad. I will put it here to see if I have made any progress .............

First, I gave ...........

1. Drag the rectangle frame. A translucent cover is located outside the red rectangle frame. By drawing four translucent rectangles, the effect can be achieved.

2. Drag the red box and stretch the red box to get the coordinates. The stretch can only be scaled out by clicking the red line above the red box and moving it to the left to zoom out. Haha, there are still many places not done, I don't want to do it anymore. I'm lazy. Do it when you have an idea .......

3. When you click a place other than the red box, the cropped image will be saved. Finally, click Save to remove the custom view and place the cropped image in the imageview. (There is an image on the underlying layer that is in the imageview itself)

Code implementation:

Custom 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;@Overridepublic 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);// 1111canvas.drawRect(0, b, 480, 800, mPaint1);canvas.drawRect(0, t, l, b, mPaint1);canvas.drawRect(r, t, 480, b, mPaint1);}@Overrideprotected 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);// 1111canvas.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);// 1111canvas.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);}}

The following is how the mainactivity class uses myview and listens to events:

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")@Overridepublic 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);}@Overridepublic 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;}}}

XML layout file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="wrap_content"    android:layout_height="wrap_content" >    <ImageView        android:id="@+id/imageview"        android:layout_width="480dip"        android:layout_height="360dip"        android:background="@drawable/aa" />    <Button        android:id="@+id/save"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="400dip"        android:text="save" />    <Button        android:id="@+id/cencal"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_marginTop="400dip"        android:text="cencal" />    <ImageView        android:id="@+id/caijian"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="400dip"        android:layout_centerHorizontal="true" /></RelativeLayout>

There are still many shortcomings ............... it would be nice if someone could give a simple method ......, the code is pretty good. Don't try it out ............

Quot ;,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.