android 大圖片拖拽並縮放實現原理

來源:互聯網
上載者:User

由於最近項目忙,部落格一直沒有時間更新,今天有點時間就連續更新兩篇吧,過過癮。

這篇圖片拖拽縮放也是我在項目中用到的,今天整理一下,將源碼奉獻給大家,希望對大家以後碰到相似的問題有協助。

這篇就不做過多介紹了,直接上源碼:

複製代碼 代碼如下:public class SpacePageActivity extends Activity {

private LinearLayout linnerLayout_spacepage;
private RelativeLayout relativeLayout_spacepage;
private Button btn_spacepage_back;
private Button btn_spacepage_save;
private static Boolean isTrue=false;

private static String IMAGE_MIME_TYPE ;

private ImageView image_spacePage;
private ProgressDialog dialog;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//隱藏標題
setContentView(R.layout.spacepage);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
initHead();

if(isNetworkAvailable()){
image_spacePage=(ImageView) findViewById(R.id.image_spacepage);
btn_spacepage_save=(Button) findViewById(R.id.btn_spacepage_save);
image_spacePage.setOnTouchListener(new MulitPointTouchListener(image_spacePage));
image_spacePage.setScaleType(ScaleType.CENTER_INSIDE);

//載入成功
String urls="http://epaper.yzdsb.com.cn/201201/04/yz2104_7.jpg";
bitmap=returnBitMap(urls);
if(null!=bitmap){
image_spacePage.setImageBitmap(bitmap);
}

}
}

public Bitmap returnBitMap(String url) {

if(null==url || "".equals(url)){
return null;
}
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setConnectTimeout(2000);
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}

return bitmap;
}
public class MulitPointTouchListener implements OnTouchListener {

Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();

public ImageView image;
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;

PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;

public MulitPointTouchListener(ImageView image) {
super();
this.image = image;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
this.image.setScaleType(ScaleType.MATRIX);

ImageView view = (ImageView) v;
// dumpEvent(event);

switch (event.getAction() & MotionEvent.ACTION_MASK) {

case MotionEvent.ACTION_DOWN:

Log.w("FLAG", "ACTION_DOWN");
matrix.set(view.getImageMatrix());
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
Log.w("FLAG", "ACTION_POINTER_DOWN");
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_UP:
Log.w("FLAG", "ACTION_UP");
case MotionEvent.ACTION_POINTER_UP:
Log.w("FLAG", "ACTION_POINTER_UP");
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
Log.w("FLAG", "ACTION_MOVE");
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY()
- start.y);
} else if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}

view.setImageMatrix(matrix);
return 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);
}
}
private void initHead() {
linnerLayout_spacepage = (LinearLayout) findViewById(R.id.linnerLayout_spacepage);
relativeLayout_spacepage=(RelativeLayout) findViewById(R.id.relativeLayout_spacepage);
btn_spacepage_back = (Button) findViewById(R.id.btn_spacepage_back);
btn_spacepage_back.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

finish();
}
});
btn_spacepage_save = (Button) findViewById(R.id.btn_spacepage_save);

}
protected boolean isNetworkAvailable() {
ConnectivityManager connectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
Log.i("NetWorkState", "Unavailabel");
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
Log.i("NetWorkState", "Availabel");
return true;
}
}
}
}
return false;
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
if(bitmap!=null){
if(!bitmap.isRecycled()){
bitmap.recycle(); //回收圖片所佔的記憶體
bitmap=null;
System.gc(); //提醒系統及時回收
}
}
super.onStop();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(bitmap!=null){
if(!bitmap.isRecycled()){
bitmap.recycle(); //回收圖片所佔的記憶體
bitmap=null;
System.gc(); //提醒系統及時回收
}
}
}
}

因為是大圖片,在onDestroy的時候要recycle掉,這樣系統才會定時回收掉,System.gc();是提醒系統回收,雖然java這種記憶體回收機制不用我們自己關心對象的回收,但是這也給android開發人員帶來了不好的一面,如果系統回收不及時很可能就造成了記憶體溢出了。什麼時候手機不擔心記憶體使用量了就好了。

運行一下看看效果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.