图片触摸旋转

第一次写博客,代码也写得不好,望各位多喷。



package com.zhaowei.picturerevolve;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity implements OnTouchListener {

	private ImageView mImageView;
	private Bitmap pic, alter;// 背景图和新绘制图
	private Canvas mCanvas;// 画布
	private Paint mPaint;// 画笔
	private Matrix mMatrix;// 矩阵
	private Point aPoint, bPoint, cPoint;// 获取手指点的位置
	private float angle = 0;// 旋转的角度
	private boolean revolveStart = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mImageView = (ImageView) findViewById(R.id.imageView1);
		pic = BitmapFactory.decodeResource(getResources(), R.drawable.revolve);
		alter = Bitmap.createBitmap(pic.getWidth(), pic.getHeight(),
				pic.getConfig());
		mCanvas = new Canvas(alter);
		mPaint = new Paint();
		mPaint.setColor(Color.BLACK);// 设置画笔颜色
		mPaint.setAntiAlias(true);// 消除锯齿
		mMatrix = new Matrix();
		mMatrix.setRotate(0, pic.getWidth() / 2, pic.getHeight() / 2);// 设置矩阵(旋转的圆点和度数)
		mCanvas.drawBitmap(pic, mMatrix, mPaint);// 往画布上画图
		mImageView.setImageBitmap(alter);// 将画好的图显示
		System.out.println("------------" + Math.PI);
		mImageView.setOnTouchListener(this);

		aPoint = new Point();
		bPoint = new Point();
		cPoint = new Point();

		aPoint.set(pic.getWidth() / 2, pic.getHeight() / 2);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			bPoint.set((int) event.getX(), (int) event.getY());
			revolveStart = true;
			System.out.println("______" + revolveStart);
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			revolveStart = false;
			break;
		case MotionEvent.ACTION_UP:
			bPoint.x = 0;
			bPoint.y = 0;
			cPoint.x = 0;
			cPoint.y = 0;
			revolveStart = false;
			break;
		case MotionEvent.ACTION_POINTER_UP:
			bPoint.x = 0;
			bPoint.y = 0;
			cPoint.x = 0;
			cPoint.y = 0;
			revolveStart = false;
			break;
		case MotionEvent.ACTION_MOVE:
			if (revolveStart) {
				cPoint.set((int) event.getX(), (int) event.getY());
				float singleAangle = (float) getAngle(aPoint, bPoint, cPoint);// 获取旋转的度数
				//触摸位置异常时设置返回值为0
				if (("NaN").equals(singleAangle + "")) {
					singleAangle = 0;
				}
				//当角度大于等于360或者小于等于-360时,将度数与360求余
				if (angle >= 360 || angle <= -360) {
					angle = angle % 360;
				}
				angle = angle + singleAangle;//新度数
				System.out.println("____________aaaaa" + aPoint.x + "___"
						+ aPoint.y);
				System.out.println("____________bbbbb" + bPoint.x + "___"
						+ bPoint.y);
				System.out.println("____________ccccc" + cPoint.x + "___"
						+ cPoint.y);
				// System.out.println("............"
				// + getAngle(aPoint, bPoint, cPoint));
				System.out.println("............" + angle);
				mMatrix.setRotate(angle, pic.getWidth() / 2,
						pic.getHeight() / 2);// 设置矩阵(旋转的圆点和度数)
				mCanvas.drawBitmap(pic, mMatrix, mPaint);// 往画布上画图
				mImageView.setImageBitmap(alter);// 将画好的图显示

				bPoint.set(cPoint.x, cPoint.y);
			}
			break;
		default:
			break;
		}
		return true;
	}

	/**
	 * @param a
	 *            点a
	 * @param b
	 *            点b
	 * @param c
	 *            点c
	 * @return a点对应的角度
	 */
	public float getAngle(Point a, Point b, Point c) {
		int aLine, bLine, cLine;
		aLine = (int) (Math.sqrt((double) ((b.x - c.x) * (b.x - c.x))
				+ (double) ((b.y - c.y) * (b.y - c.y))));
		bLine = (int) (Math.sqrt((double) ((c.x - a.x) * (c.x - a.x))
				+ (double) ((c.y - a.y) * (c.y - a.y))));
		cLine = (int) (Math.sqrt((double) ((a.x - b.x) * (a.x - b.x))
				+ (double) ((a.y - b.y) * (a.y - b.y))));
		System.out.println("------------aaaaa---" + aLine);
		System.out.println("------------bbbbb---" + bLine);
		System.out.println("------------ccccc---" + cLine);
		// 判断点b相对于点a在第几象限,并判断是逆时针还是顺时针旋转
		if (b.x > a.x && b.y < a.y) {
			if (c.y < b.y && c.x < b.x) {
				return (float) -((Math.acos((Math.pow(bLine, 2)
						+ Math.pow(cLine, 2) - Math.pow(aLine, 2))
						/ (2 * bLine * cLine))) / 3.14 * 180);
			} else if (c.y > b.y && c.x > b.x) {
				return (float) ((Math.acos((Math.pow(bLine, 2)
						+ Math.pow(cLine, 2) - Math.pow(aLine, 2))
						/ (2 * bLine * cLine))) / 3.14 * 180);
			} else {
				return 0;
			}
		} else if (b.x < a.x && b.y < a.y) {
			if (c.y > b.y && c.x < b.x) {
				return (float) -((Math.acos((Math.pow(bLine, 2)
						+ Math.pow(cLine, 2) - Math.pow(aLine, 2))
						/ (2 * bLine * cLine))) / 3.14 * 180);
			} else if (c.y < b.y && c.x > b.x) {
				return (float) ((Math.acos((Math.pow(bLine, 2)
						+ Math.pow(cLine, 2) - Math.pow(aLine, 2))
						/ (2 * bLine * cLine))) / 3.14 * 180);
			} else {
				return 0;
			}
		} else if (b.x < a.x && b.y > a.y) {
			if (c.x > b.x && c.y > b.y) {
				return (float) -((Math.acos((Math.pow(bLine, 2)
						+ Math.pow(cLine, 2) - Math.pow(aLine, 2))
						/ (2 * bLine * cLine))) / 3.14 * 180);
			} else if (c.x < b.x && c.y < b.y) {
				return (float) ((Math.acos((Math.pow(bLine, 2)
						+ Math.pow(cLine, 2) - Math.pow(aLine, 2))
						/ (2 * bLine * cLine))) / 3.14 * 180);
			} else {
				return 0;
			}
		} else if (b.x > a.x && b.y > a.y) {
			if (c.x > b.x && c.y < b.y) {
				return (float) -((Math.acos((Math.pow(bLine, 2)
						+ Math.pow(cLine, 2) - Math.pow(aLine, 2))
						/ (2 * bLine * cLine))) / 3.14 * 180);
			} else if (c.x < b.x && c.y > b.y) {
				return (float) ((Math.acos((Math.pow(bLine, 2)
						+ Math.pow(cLine, 2) - Math.pow(aLine, 2))
						/ (2 * bLine * cLine))) / 3.14 * 180);
			} else {
				return 0;
			}
		} else {
			return 0;
		}
	}
}

下面是xml代码


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.zhaowei.picturerevolve.MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

代码中返回角度的时候老是出问题,所以在返回之后,我把值做了一个简单的处理,返回角度这一段有点儿绕,我也没有特别仔细的去思考问题出现的原因



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值