由于最近项目忙,博客一直没有时间更新,今天有点时间就连续更新两篇吧,过过瘾。
这篇图片拖拽缩放也是我在项目中用到的,今天整理一下,将源码奉献给大家,希望对大家以后碰到相似的问题有帮助。
这篇就不做过多介绍了,直接上源码:
复制代码 代码如下:
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开发人员带来了不好的一面,如果系统回收不及时很可能就造成了内存溢出了。什么时候手机不担心内存使用了就好了。
运行一下看看效果:
以上是内存溢出为你收集整理的android 大图片拖拽并缩放实现原理全部内容,希望文章能够帮你解决android 大图片拖拽并缩放实现原理所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)