添加放大缩小功能
我已经编写了这段代码,用于从 url 加载多个图像并在图像上滑动效果......但现在我需要放大并放大图像。手指展开的图像...我想捏缩放...我无法实现缩放功能..也对在哪里放置该功能感到困惑,或者我应该为此上课..请帮助....谢谢并问候..
这是我的代码:
package com.conn;
import android.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class swipe_touch extends Activity {
private static final String LOGID = "CHECKTHISOUT";
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;
private ViewFlipper viewFlipper;
int i;
ImageView[] img = new ImageView[24];
String message = "Initial Message";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
//Set up viewflipper
try
{
viewFlipper = new ViewFlipper(this);
for(i=1;i<=3;i++)
{
Log.i("for","loop");
j_comm_func funs = new j_comm_func();
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
Bitmap bm = funs.LoadImage("http://65.175.77.34/newsnow/2462011/largest3/2462011-md-hr-"+i+".jpg", bmOptions);
img[i] = new ImageView(this);
img[i].setImageBitmap(bm);
viewFlipper.addView(img[i]);
}
}
catch(Exception e)
{
Log.i("error",e.getMessage());
}
//set up animations
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_out_right);
slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_out_right);
//put up a brownie as a starter
setContentView(viewFlipper);
gestureDetector = new GestureDetector(new MyGestureDetector());
}
public class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.v(LOGID,"right to left swipe detected");
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
setContentView(viewFlipper);
} // left to right swipe
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.v(LOGID,"left to right swipe detected");
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.showPrevious();
setContentView(viewFlipper);
}
} catch (Exception e) {
// nothing
}
return false;
}
}
// This doesn't work
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)){
Log.v(LOGID,"screen touched");
return true;
}
else{
return false;
}
}
}
i have written this code for loading multiple of images from url one by and swiping effect on images.....but now i need to zoom in & out the images on finger spread...i think pinch zoom...i am unable to implement the zooming function..also confused about where to place the function or i should make a class for this..please help....thanks and regards..
this is my code:
package com.conn;
import android.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class swipe_touch extends Activity {
private static final String LOGID = "CHECKTHISOUT";
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private Animation slideLeftIn;
private Animation slideLeftOut;
private Animation slideRightIn;
private Animation slideRightOut;
private ViewFlipper viewFlipper;
int i;
ImageView[] img = new ImageView[24];
String message = "Initial Message";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
//Set up viewflipper
try
{
viewFlipper = new ViewFlipper(this);
for(i=1;i<=3;i++)
{
Log.i("for","loop");
j_comm_func funs = new j_comm_func();
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
Bitmap bm = funs.LoadImage("http://65.175.77.34/newsnow/2462011/largest3/2462011-md-hr-"+i+".jpg", bmOptions);
img[i] = new ImageView(this);
img[i].setImageBitmap(bm);
viewFlipper.addView(img[i]);
}
}
catch(Exception e)
{
Log.i("error",e.getMessage());
}
//set up animations
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_out_right);
slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_out_right);
//put up a brownie as a starter
setContentView(viewFlipper);
gestureDetector = new GestureDetector(new MyGestureDetector());
}
public class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.v(LOGID,"right to left swipe detected");
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
setContentView(viewFlipper);
} // left to right swipe
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.v(LOGID,"left to right swipe detected");
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.showPrevious();
setContentView(viewFlipper);
}
} catch (Exception e) {
// nothing
}
return false;
}
}
// This doesn't work
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)){
Log.v(LOGID,"screen touched");
return true;
}
else{
return false;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将图像加载到 WebView 中,该视图内置了捏合缩放功能。
Load the images in a WebView, which has the pinch to zoom functionality built in.