android 雙擊圖片放大縮小
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);
}
}
最後更新:2017-04-02 16:47:37