android 双击图片放大缩小
2012-06-07 14:42
323 查看
package com.szxys.doubleclike; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.provider.ContactsContract.CommonDataKinds.Im; import android.view.View; import android.view.animation.Animation; import android.view.animation.ScaleAnimation; import android.widget.ImageView; public class DoubleClickActivity extends Activity { /** Called when the activity is first created. */ private ImageView image; private Bitmap bitmap; private boolean isbig = false; private int count = 2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); image = (ImageView) findViewById(R.id.image); bitmap = BitmapFactory .decodeResource(getResources(), R.drawable.girl_1); image.setBackgroundResource(R.drawable.girl_1); image.setOnClickListener(new clike()); } private long sTime = 0; private long eTime = 0; private Drawable drawable; interface TimeListener{ public long getCurrentTime(); } class clike implements View.OnClickListener, TimeListener{ @Override public long getCurrentTime() { // TODO Auto-generated method stub return System.currentTimeMillis(); } @Override public void onClick(View v) { // TODO Auto-generated method stub if(sTime!=0&&(getCurrentTime()-sTime>500)){ sTime = 0; count=2; } if (!isbig) { --count; if (count > 0) { sTime = getCurrentTime(); System.out.println(sTime + "--s--"); } if (count == 0) { eTime = getCurrentTime(); System.out.println(sTime + "--e--"); if ((eTime - sTime) <= 500) { drawable = resizeImage(bitmap, bitmap.getWidth() * 4, bitmap.getHeight() * 4); image.setBackgroundDrawable(drawable); ScaleAnimation animation = new ScaleAnimation(0.25f, 1.0f, 0.25f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f); animation.setDuration(500); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub isbig = true; sTime = 0; eTime = 0; count = 2; } }); image.setAnimation(animation); System.out.println("----------haha--------------"); } } }else{ --count; if (count > 0) { sTime = getCurrentTime(); System.out.println(sTime + "--s--"); } if (count == 0) { eTime = getCurrentTime(); System.out.println(sTime + "--e--"); Drawable Tempdrawable = drawable; final Bitmap bitmap = ((BitmapDrawable)Tempdrawable).getBitmap(); if ((eTime - sTime) <= 500) { drawable = resizeImage(bitmap, bitmap.getWidth()/4, bitmap.getHeight()/4); ScaleAnimation animation = new ScaleAnimation(1.0f, 0.25f, 1.0f, 0.25f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f); animation.setDuration(500); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { image.clearAnimation(); image.setBackgroundDrawable(drawable); isbig = false; sTime = 0; eTime = 0; count = 2; } }); image.setBackgroundDrawable(Tempdrawable); image.setAnimation(animation); System.out.println("----------haha--------------"); } } } } } // 压缩图片到指定大小 public static Drawable resizeImage(Bitmap bitmap, int w, int h) { Bitmap BitmapOrg = bitmap; int width = BitmapOrg.getWidth(); int height = BitmapOrg.getHeight(); int newWidth = w; int newHeight = h; float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true); return new BitmapDrawable(resizedBitmap); } }
相关文章推荐
- Android-图片预览(自定义ImageView 封装(jar): 图片放大缩小,自由移动,双击放大,多点触控)
- Android------实现图片双击放大,缩小,左右滑动的多种方式
- Android实现ImageView图片双击放大及缩小
- Android实现ImageView图片双击放大及缩小
- Android自定义ImageView(二)——实现双击放大与缩小图片
- Android图片查看支持双击放大缩小、多点触摸(多机型测试,长期使用很稳定)
- Android App中实现可以双击放大和缩小图片功能的实例
- android 双击图片放大,再双击缩小效果 【安卓进化九】
- Android图片查看支持双击放大缩小、多点触摸
- Android图片查看支持双击放大缩小、多点触摸
- android 图片浏览功能 图片放大缩小 使用 photoview 双击或双指缩放的ImageView
- Android自定义ImageView实现图片缩放滑动,双击放大缩小,多点触控旋转,圆角矩形,圆形和仿刮刮卡效果
- Android------实现图片双击放大,缩小,左右滑动的多种方式
- Android------实现图片双击放大,缩小,左右滑动的多种方式
- Android图片查看支持双击放大缩小、多点触摸
- Android自定义ImageView实现图片缩放滑动,双击放大缩小,多点触控缩放
- Android 图片拖拽、放大缩小的自定义控件
- Android中图片的放大与缩小
- android Matrix图片随意的放大缩小,拖动,翻页(续)
- android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果