사용자 정의 ImageView 지원 크기 조정, 드래그, 재 활용
package com.example.myimageview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class MyImageView extends ImageView{
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
/** */
private Bitmap bitmap = null;
/** */
private DisplayMetrics dm;
/** */
float minScaleR = 1.0f;
/** */
static final float MAX_SCALE = 15f;
/** */
static final int NONE = 0;
/** */
static final int DRAG = 1;
/** */
static final int ZOOM = 2;
/** */
int mode = NONE;
/** float x,y , X Y*/
PointF prev = new PointF();
PointF mid = new PointF();
float dist = 1f;
public MyImageView(Context context) {
super(context);
setupView();
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
setupView();
}
public void setupView(){
Context context = getContext();
// ,
dm = context.getResources().getDisplayMetrics();
// MyImageView bitmap
BitmapDrawable bd = (BitmapDrawable)this.getDrawable();
if(bd != null){
bitmap = bd.getBitmap();
}
// ScaleType ScaleType.MATRIX,
this.setScaleType(ScaleType.MATRIX);
this.setImageBitmap(bitmap);
//bitmap center
if(bitmap != null){
center(true, true);
}
this.setImageMatrix(matrix);
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
//
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
prev.set(event.getX(), event.getY());
mode = DRAG;
break;
//
case MotionEvent.ACTION_POINTER_DOWN:
dist = spacing(event);
// 10,
if (spacing(event) > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_UP:{
break;
}
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
//savedMatrix.set(matrix);
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - prev.x, event.getY()
- prev.y);
} else if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float tScale = newDist / dist;
matrix.postScale(tScale, tScale, mid.x, mid.y);
}
}
break;
}
MyImageView.this.setImageMatrix(matrix);
CheckView();
return true;
}
});
}
/**
* 、
*/
protected void center(boolean horizontal, boolean vertical) {
Matrix m = new Matrix();
m.set(matrix);
RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
m.mapRect(rect);
float height = rect.height();
float width = rect.width();
float deltaX = 0, deltaY = 0;
if (vertical) {
// , 。 , ,
int screenHeight = dm.heightPixels;
if (height < screenHeight) {
deltaY = (screenHeight - height) / 2 - rect.top;
} else if (rect.top > 0) {
deltaY = -rect.top;
} else if (rect.bottom < screenHeight) {
deltaY = this.getHeight() - rect.bottom;
}
}
if (horizontal) {
int screenWidth = dm.widthPixels;
if (width < screenWidth) {
deltaX = (screenWidth - width) / 2 - rect.left;
} else if (rect.left > 0) {
deltaX = -rect.left;
} else if (rect.right < screenWidth) {
deltaX = screenWidth - rect.right;
}
}
matrix.postTranslate(deltaX, deltaY);
}
/**
* ,
*/
private void CheckView() {
float p[] = new float[9];
matrix.getValues(p);
if (mode == ZOOM) {
if (p[0] < minScaleR) {
//Log.d("", " :"+p[0]+", :"+minScaleR);
matrix.setScale(minScaleR, minScaleR);
}
if (p[0] > MAX_SCALE) {
//Log.d("", " :"+p[0]+", :"+MAX_SCALE);
matrix.set(savedMatrix);
}
}
center(true, true);
}
/**
*
*/
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);
}
}
레이아웃 파일 은 주의해 야 합 니 다. < com. example. my mageview. MyImageView > < / com. example. my mageview. MyImageView > 탭 을 사용 하 십시오. 초보 자 들 이 모 를 까 봐 잔소리 하지 마 세 요.
<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" >
<com.example.myimageview.MyImageView
android:id="@+id/imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/item" >
</com.example.myimageview.MyImageView>
</RelativeLayout>
MainActivity 를 새로 만 들 었 습 니 다.
package com.example.myimageview;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyImageView myImageView = (MyImageView)findViewById(R.id.imageview);
myImageView.setImageDrawable(getResources().getDrawable(R.drawable.item1));
}
}
demo 다운로드, 포인트 없 이 다운로드 가능
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.