当前位置: 移动技术网 > 移动技术>移动开发>Android > android 大图片拖拽并缩放实现原理

android 大图片拖拽并缩放实现原理

2019年07月24日  | 移动技术网移动技术  | 我要评论
由于最近项目忙,博客一直没有时间更新,今天有点时间就连续更新两篇吧,过过瘾。 这篇图片拖拽缩放也是我在项目中用到的,今天整理一下,将源码奉献给大家,希望对大家以后碰到相似

由于最近项目忙,博客一直没有时间更新,今天有点时间就连续更新两篇吧,过过瘾。

这篇图片拖拽缩放也是我在项目中用到的,今天整理一下,将源码奉献给大家,希望对大家以后碰到相似的问题有帮助。

这篇就不做过多介绍了,直接上源码:

复制代码 代码如下:

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开发人员带来了不好的一面,如果系统回收不及时很可能就造成了内存溢出了。什么时候手机不担心内存使用了就好了。

运行一下看看效果:


如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网