关闭

自定义View(多点触控)

130人阅读 评论(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"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity中

package com.example.day04_touch;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
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.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainActivity extends Activity {

    SurfaceHolder holder;
    private int width;
    private int height;
    private Bitmap bitmap;
    private int imageW;
    private int imageH;
    private int x;
    private int y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MySurface(this));

        // 获取屏幕的宽和高 DisplayMetrics类专门获取像素值
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels; // 屏幕的宽度(像素值)
        height = metrics.heightPixels; // 屏幕的高度(像素值)

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);

        // 得到图片的宽高
        imageW = bitmap.getWidth();
        imageH = bitmap.getHeight();

        // 设置图片的位置
        x = (width - imageW) / 2;
        y = (height - imageH) / 2;

    }

    // 绘制图片
    public void setimage(float scale, int bwidth, int bheight) {
        // 得到画布
        Canvas canvas = holder.lockCanvas();
        // 设置画布的颜色
        canvas.drawColor(Color.BLACK);
        // 设置画笔
        Paint paint = new Paint();

        // 画矩形
        canvas.drawRect(0, 0, bwidth, bheight, paint);

        // 得到Matrix类 用于控制图片的缩放
        Matrix matrix = new Matrix();

        // 设置缩放比例
        matrix.postScale(scale, scale);

        // 得到缩放后的bitmap图片
        Bitmap bafter = Bitmap.createBitmap(bitmap, 0, 0, bwidth, bheight,
                matrix, true);

        // 得到新的bitmap的宽度和高度并且重新计算位置
        imageW = bafter.getWidth();
        imageH = bafter.getHeight();

        // 重新计算图片的位置
        x = (bwidth - imageW) / 2;
        y = (bheight - imageH) / 2;

        // 平移过去
        canvas.translate(x, y);
        // 重新绘制bitmap
        canvas.drawBitmap(bitmap, matrix, paint);
        // 解锁,把画布显示到屏幕上
        holder.unlockCanvasAndPost(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 获得触控点个数
        int pointerCount = event.getPointerCount();
        if (pointerCount == 2) {
            // 获取两个点的距离
            float pointA = event.getX(0);
            float pointB = event.getX(1);
            // 判断哪个大
            // 如果第一个点小于第二个点就调换位置
            if (pointA < pointB) {
                float t = pointA;
                pointA = pointB;
                pointB = t;

            }

            // 只要手指不抬起,就进行缩放
            if (!(event.getAction() == MotionEvent.ACTION_UP)) {
                float scale = getScale(pointA, pointB) / 3;
                setimage(scale, bitmap.getWidth(), bitmap.getHeight());
            }
        }

        return super.onTouchEvent(event);
    }

    // 缩放比
    public float getScale(float pointA, float pointB) {
        // 缩放比例
        float scale = pointA / pointB;

        return scale;

    }

    class MySurface extends SurfaceView implements SurfaceHolder.Callback {

        public MySurface(Context context) {
            super(context);

            // 获得SurfaceHolder对象
            holder = getHolder();
            // 为SurfaceView添加状态监听
            holder.addCallback(this);
            // 设置焦点
            setFocusable(true);
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // TODO Auto-generated method stub

        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            setimage(1.0f, bitmap.getWidth(), bitmap.getHeight());
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub

        }

    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9281次
    • 积分:397
    • 等级:
    • 排名:千里之外
    • 原创:32篇
    • 转载:4篇
    • 译文:1篇
    • 评论:0条