Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> android 圖片的平移,縮放和旋轉,

android 圖片的平移,縮放和旋轉,

編輯:關於android開發

android 圖片的平移,縮放和旋轉,


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
    <!-- 定義TextView的文本標簽 -->
    <TextView
        android:id="@+id/Tv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="點擊屏幕,隱藏或顯示導航欄!" />
</RelativeLayout>
MyView.java
package com.example.yanlei.yl;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.DisplayMetrics;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.widget.ImageView;
//自定義MyView類繼承自ImageView
public class MyView extends ImageView {
    private float x_down = 0;
    private float y_down = 0;
    //起始點的坐標
    private PointF start = new PointF();
    //中心點的坐標
    private PointF mid = new PointF();
    private float oldDist = 1f;
    private float oldRotation = 0;
    private Matrix matrix = new Matrix();
    private Matrix matrix1 = new Matrix();
    private Matrix savedMatrix = new Matrix();

    private static final int NONE = 0;
    private static final int DRAG = 1;
    private static final int ZOOM = 2;
    private int mode = NONE;

    private boolean matrixCheck = false;

    //記錄當前屏幕的寬度
    private int widthScreen;
    //記錄當前屏幕的高度
    private int heightScreen;

    //在頁面中顯示的Bitmap圖片
    private Bitmap kenan;

    public MyView(Activity activity) {
        super(activity);
        //通過Bitampfactory讀取drawable目錄下的kenan資源
        kenan = BitmapFactory.
                decodeResource(getResources(), R.drawable.kenan);

        //定義圖片一個顯示矩陣
        DisplayMetrics dm = new DisplayMetrics();
        //得到當前屏幕的顯示矩陣存入dm變量
        activity.getWindowManager().
                getDefaultDisplay().getMetrics(dm);
        //通過顯示矩陣得到當前屏幕的寬度和高度的像素值
        widthScreen = dm.widthPixels;
        heightScreen = dm.heightPixels;

        matrix = new Matrix();
    }

    //顯示view的時候回調onDraw
    protected void onDraw(Canvas canvas) {
        //首先保存當前頁面已有的圖像
        canvas.save();
        //按照當前的矩陣繪制kenan圖片
        canvas.drawBitmap(kenan, matrix, null);
        //畫圖板恢復
        canvas.restore();
    }

    //當用戶觸摸此視圖的時候回調次方法
    public boolean onTouchEvent(MotionEvent event) {
        //得到touch的事件類型
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                //當按下屏幕時,記錄當前的狀態為拖動
                mode = DRAG;
                //記錄xy坐標
                x_down = event.getX();
                y_down = event.getY();
                //保存當前的矩陣
                savedMatrix.set(matrix);
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                //多個手指觸摸的狀態
                mode = ZOOM;
                //記錄之前的兩手指間距
                oldDist = spacing(event);
                //記錄之前的角度
                oldRotation = rotation(event);
                //保存當前的圖片矩陣
                savedMatrix.set(matrix);
                //得到旋轉的中心點
                midPoint(mid, event);
                break;
            case MotionEvent.ACTION_MOVE:
                //當手指移動時的狀態
                if (mode == ZOOM) {
                    //縮放並且平移
                    matrix1.set(savedMatrix);
                    //得到旋轉的角度
                    float rotation =
                            rotation(event) - oldRotation;
                    //得到距離
                    float newDist = spacing(event);
                    //得到放大倍數
                    float scale = newDist / oldDist;
                    //縮放倍數
                    matrix1.postScale(scale, scale, mid.x, mid.y);
                    //得到旋轉角度
                    matrix1.postRotate(rotation, mid.x, mid.y);
                    //得到圖片是否出邊界
                    matrixCheck = matrixCheck();
                    if (matrixCheck == false) {
                        matrix.set(matrix1);
                        invalidate();
                    }
                } else if (mode == DRAG) {
                    //平行移動
                    matrix1.set(savedMatrix);
                    matrix1.postTranslate(event.getX() - x_down
                            , event.getY() - y_down);// 平移
                    matrixCheck = matrixCheck();
                    matrixCheck = matrixCheck();
                    if (matrixCheck == false) {
                        matrix.set(matrix1);
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                break;
        }
        return true;
    }

    //對圖片的矩陣進行檢測
    private boolean matrixCheck() {
        float[] f = new float[9];
        matrix1.getValues(f);
        // 圖片4個頂點的坐標
        float x1 = f[0] * 0 + f[1] * 0 + f[2];
        float y1 = f[3] * 0 + f[4] * 0 + f[5];
        float x2 = f[0] * kenan.getWidth()
                + f[1] * 0 + f[2];
        float y2 = f[3] * kenan.getWidth()
                + f[4] * 0 + f[5];
        float x3 = f[0] * 0 + f[1] *
                kenan.getHeight() + f[2];
        float y3 = f[3] * 0 + f[4] *
                kenan.getHeight() + f[5];
        float x4 = f[0] * kenan.getWidth() +
                f[1] * kenan.getHeight() + f[2];
        float y4 = f[3] * kenan.getWidth() +
                f[4] * kenan.getHeight() + f[5];
        // 圖片現寬度
        double width = Math.sqrt((x1 - x2) *
                (x1 - x2) + (y1 - y2) * (y1 - y2));
        // 縮放比率判斷
        if (width < widthScreen / 3 || width > widthScreen * 3) {
            return true;
        }
        // 出界判斷
        if ((x1 < widthScreen / 3 && x2 < widthScreen / 3
                && x3 < widthScreen / 3
                && x4 < widthScreen / 3)
                || (x1 > widthScreen * 2 / 3
                && x2 > widthScreen * 2 / 3
                && x3 > widthScreen * 2 / 3
                && x4 > widthScreen * 2 / 3)
                || (y1 < heightScreen / 3
                && y2 < heightScreen / 3
                && y3 < heightScreen / 3
                && y4 < heightScreen / 3)
                || (y1 > heightScreen * 2 / 3
                && y2 > heightScreen * 2 / 3
                && y3 > heightScreen * 2 / 3
                && y4 > heightScreen * 2 / 3)) {
            return true;
        }
        return false;
    }

    // 觸碰兩點間距離
    private float spacing(MotionEvent event) {
        //通過三角函數得到兩點間的距離
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
    }

    // 取手勢中心點
    private void midPoint(PointF point, MotionEvent event) {
        //得到手勢中心點的位置
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x / 2, y / 2);
    }

    // 取旋轉角度
    private float rotation(MotionEvent event) {
        //得到兩個手指間的旋轉角度
        double delta_x = (event.getX(0) - event.getX(1));
        double delta_y = (event.getY(0) - event.getY(1));
        double radians = Math.atan2(delta_y, delta_x);
        return (float) Math.toDegrees(radians);
    }
}

 

MainActivity

package com.example.yanlei.yl;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.text.Html;
import android.text.Html.ImageGetter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Button;


import android.app.Activity;
import android.content.Intent;

import android.view.MotionEvent;
import android.widget.TextView;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends AppCompatActivity {
    //定義TextView對象
    private TextView Tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //定義自定義View的對象
        MyView myview = new MyView(this);
        //設置當前頁面的視圖為自定義的myview
        setContentView(myview);


    }


}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved