android ImageView 问题

GG思密达12138 发布于 2016/05/11 10:41
阅读 113
收藏 0


//错误在黄色背景

package com.example.gesturezoom;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.ImageView;


public class MainActivity extends Activity implements OnGestureListener{
    ImageView imageView;
    Bitmap bitmap;
    GestureDetector gestureDetector;
//    设置图片的高和宽
    int WIDTH, HEIGHT;
    Matrix matrix;
//    设置缩放比例
    float currentScale=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gestureDetector = new GestureDetector(this, this);
        matrix = new Matrix();
        imageView = (ImageView) findViewById(R.id.show);
        bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.flower);
        WIDTH = bitmap.getWidth();
        HEIGHT = bitmap.getHeight();
        imageView.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.flower));
//        imageView.setImageBitmap(bitmap);   为什么应用这个语句程序运行就会出现错误
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        velocityX = velocityX>4000?4000:velocityX;
        velocityX = velocityX<-4000?-4000:velocityX;
        currentScale += currentScale*velocityX/4000.0f;
        currentScale = currentScale>0.01?currentScale:0.01f;

        matrix.reset();
        matrix.setScale(currentScale, currentScale, WIDTH/2,HEIGHT/2);
        BitmapDrawable tmp = (BitmapDrawable) imageView.getDrawable();
        if (!tmp.getBitmap().isRecycled())
        {
            tmp.getBitmap().recycle();
        }
        Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, 0,WIDTH, HEIGHT, matrix, true);
        imageView.setImageBitmap(bitmap1);
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
}
加载中
返回顶部
顶部