Drawable文件夹中的ImageUri

时间:2016-09-11 03:05:10

标签: image uri zoom drawable pan

我一直在学习本教程,以了解如何在我正在开发的应用中实现Pinch Zoom和Pan功能 https://www.youtube.com/watch?v=BY8hLhu50po&list=PL9jCwTXYWjDJjDE_JxRozYGKGt8gbUXg7

基本上,他从图库中加载图像并将其显示在支持缩放/平移功能的图像视图中。

我想将图片预加载到图片视图中,而不是从图库中选择一个。理想情况下,我想从drawable加载图像。

教程的应用程序有很多额外的功能,我试图清除它们 首先它打开一个画廊。然后选择要显示的图像,并将其显示为mImageView内的缩略图。 在长按时,它隐藏了mImageView并在第二个图像视图中显示最大化的图像。 (MPinchZoomImageView)

此时,图像支持缩放和平移功能。

如果可以,我想跳过图库和第一个图像视图,只使用PinchZoom图像视图。我不确定我是怎么做的。

代码:

ImageViewMainActivity

public class ImageViewMainActivity extends AppCompatActivity {

ImageView mImageView;
PinchZoomImageView mPinchZoomImageView;
private Uri mImageUri;

private static final int REQUEST_OPEN_RESULT_CODE = 0;

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

    mImageView = (ImageView) findViewById(R.id.imageView);
    mPinchZoomImageView = (PinchZoomImageView) findViewById(R.id.pinchZoomImageView);

    mImageView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

           pinchZoomPan();
            return true;
        }
    });

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("image/*");
    startActivityForResult(intent, REQUEST_OPEN_RESULT_CODE); // pass it a context of 0
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    View decorView = getWindow().getDecorView();
    if(hasFocus) {
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        );
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if(requestCode == REQUEST_OPEN_RESULT_CODE && resultCode == RESULT_OK) {
        if(resultData != null) {
            mImageUri = resultData.getData();

            Glide.with(this)
                    .load(mImageUri)
                    .into(mImageView);
        }
    }
}


private void pinchZoomPan() {
    mPinchZoomImageView.setImageUri(mImageUri);
    mImageView.setAlpha(0.f); // set mImageView invisible.
    mPinchZoomImageView.setVisibility(View.VISIBLE);
}
}

PinchZoomImageView

public class PinchZoomImageView extends ImageView {                                                                                                 

private Bitmap mBitmap;                                                                                                                         
private int mImageWidth;                                                                                                                        
private int mImageHeight;                                                                                                                       
private final static float mMinZoom = 1.f;                                                                                                      
private final static float mMaxZoom = 4.f;                                                                                                      
private float mScaleFactor = 1.f;                                                                                                               
private ScaleGestureDetector mScaleGestureDetector;                                                                                             
private final static int NONE = 0;                                                                                                              
private final static int PAN = 1;                                                                                                               
private final static int ZOOM = 2;                                                                                                              
private int mEventState;                                                                                                                        
private float mStartX = 0;                                                                                                                      
private float mStartY = 0;                                                                                                                      
private float mTranslateX = 0;                                                                                                                  
private float mTranslateY = 0;                                                                                                                  
private float mPreviousTranslateX = 0;                                                                                                          
private float mPreviousTranslateY = 0;                                                                                                          

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {                                                         

    @Override                                                                                                                                   
    public boolean onScale(ScaleGestureDetector detector) {                                                                                     
        mScaleFactor *= detector.getScaleFactor();                                                                                              
        mScaleFactor = Math.max(mMinZoom, Math.min(mMaxZoom, mScaleFactor));                                                                    
        // invalidate();                                                                                                                        
        // requestLayout();                                                                                                                     
        return super.onScale(detector);                                                                                                         
    }                                                                                                                                           
}                                                                                                                                               

public PinchZoomImageView(Context context, AttributeSet attrs) {                                                                                
    super(context, attrs);                                                                                                                      

    mScaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleListener());                                                        
}                                                                                                                                               

@Override                                                                                                                                       
public boolean onTouchEvent(MotionEvent event) {                                                                                                

    switch(event.getAction() & MotionEvent.ACTION_MASK) {                                                                                       
        case MotionEvent.ACTION_DOWN:                                                                                                           
            mEventState = PAN;                                                                                                                  
            mStartX = event.getX() - mPreviousTranslateX;                                                                                       
            mStartY = event.getY() - mPreviousTranslateY;                                                                                       
            break;                                                                                                                              
        case MotionEvent.ACTION_UP:                                                                                                             
            mEventState = NONE;                                                                                                                 
            mPreviousTranslateX = mTranslateX;                                                                                                  
            mPreviousTranslateY = mTranslateY;                                                                                                  
            break;                                                                                                                              
        case MotionEvent.ACTION_MOVE:                                                                                                           
            mTranslateX = event.getX() - mStartX;                                                                                               
            mTranslateY = event.getY() - mStartY;                                                                                               
            break;                                                                                                                              
        case MotionEvent.ACTION_POINTER_DOWN:                                                                                                   
            mEventState = ZOOM;                                                                                                                 
            break;                                                                                                                              
    }                                                                                                                                           
    mScaleGestureDetector.onTouchEvent(event);                                                                                                  
    if((mEventState == PAN && mScaleFactor != mMinZoom) || mEventState == ZOOM) {          // called under the condition that window is zoomed i
        invalidate();                                                                                                                           
        requestLayout();                                                                                                                        
    }                                                                                                                                           
    return true;                                                                                                                                
}                                                                                                                                               

@Override                                                                                                                                       
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {                                                                         
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);                                                                                       

    int imageWidth = MeasureSpec.getSize(widthMeasureSpec);                                                                                     
    int imageHeight = MeasureSpec.getSize(heightMeasureSpec);                                                                                   
    int scaledWidth = Math.round(mImageWidth * mScaleFactor);                                                                                   
    int scaledHeight = Math.round(mImageHeight * mScaleFactor);                                                                                 

    setMeasuredDimension(                                                                                                                       
            Math.min(imageWidth, scaledWidth),                                                                                                  
            Math.min(imageHeight, scaledHeight)                                                                                                 
    );                                                                                                                                          
}                                                                                                                                               

@Override                                                                                                                                       
protected void onDraw(Canvas canvas) {                                                                                                          
    super.onDraw(canvas);                                                                                                                       

    canvas.save();                                                                                                                              
    canvas.scale(mScaleFactor, mScaleFactor);                                                                                                   
    // canvas.scale(mScaleFactor, mScaleFactor, mScaleGestureDetector.getFocusX(), mScaleGestureDetector.getFocusY());                          
    if((mTranslateX * -1) < 0) {                                                                                                                
        mTranslateX = 0;                                                                                                                        
    } else if ((mTranslateX * -1) > mImageWidth * mScaleFactor - getWidth()) {                                                                  
        mTranslateX = (mImageWidth * mScaleFactor - getWidth()) * -1;                                                                           
    }                                                                                                                                           
    if((mTranslateY * -1) < 0) {                                                                                                                
        mTranslateY = 0;                                                                                                                        
    } else if ((mTranslateY * -1) > mImageHeight * mScaleFactor - getHeight()) {                                                                
        mTranslateY = (mImageHeight * mScaleFactor - getHeight()) * -1;                                                                         
    }                                                                                                                                           
    canvas.translate(mTranslateX/mScaleFactor, mTranslateY/mScaleFactor);                                                                       
    canvas.drawBitmap(mBitmap, 0, 0, null);                                                                                                     
    canvas.restore();                                                                                                                           
}                                                                                                                                               

@Override                                                                                                                                       
protected void onSizeChanged(int w, int h, int oldw, int oldh) {                                                                                
    super.onSizeChanged(w, h, oldw, oldh);                                                                                                      
}                                                                                                                                               

public void setImageUri(Uri uri) {                                                                                                              
    try {                                                                                                                                       
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), uri);                                              
        float aspecRatio = (float) bitmap.getHeight() / (float) bitmap.getWidth();                                                              
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();                                                                     
        mImageWidth = displayMetrics.widthPixels;                                                                                               
        mImageHeight = Math.round(mImageWidth * aspecRatio);                                                                                    

        mBitmap = Bitmap.createScaledBitmap(bitmap, mImageWidth, mImageHeight, false);                                                          
        invalidate();                                                                                                                           
        requestLayout();                                                                                                                        
    } catch (IOException e) {                                                                                                                   
        e.printStackTrace();                                                                                                                    
    }                                                                                                                                           
}                                                                                                                                               
}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mobapptut.com.imageviewer.ImageViewMainActivity">

<ImageView
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:id="@+id/imageView"
    android:layout_centerInParent="true" />

<mobapptut.com.imageviewer.PinchZoomImageView
    android:visibility="invisible"
    android:id="@+id/pinchZoomImageView"
    android:layout_centerInParent="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</RelativeLayout>    

谢谢

0 个答案:

没有答案