Android 图片缩放,手势,事件

我们这里使用第三方的jar包,来完成。

下载地址

xml

"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="${packageName}.${activityClass}" >

    .co.senab.photoview.PhotoView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    .co.senab.photoview.PhotoView>

Java

package com.example.photoviewdemo;

import java.io.File;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher.OnPhotoTapListener;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

    private PhotoView photoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        photoView = (PhotoView) findViewById(R.id.image);
        String filePath = Environment.getExternalStorageDirectory() + "/1.jpg";
        File file = new File(filePath);
        if (file.exists()) {
            Bitmap bitmap = BitmapFactory.decodeFile(filePath);
            photoView.setImageBitmap(bitmap);
        }
        photoView.setOnPhotoTapListener(new OnPhotoTapListener() {

            @Override
            public void onPhotoTap(View arg0, float arg1, float arg2) {

            }
        });
    }
}

可实现图片的双击放大,单机缩小,手势放大缩小等。

你可能感兴趣的:(Android)