gpt4 book ai didi

android - 向鳍状肢添加多个 ImageView 并将图像从 url 加载到这些 ImageView

转载 作者:行者123 更新时间:2023-11-30 04:38:11 28 4
gpt4 key购买 nike

我是 android 的新手,正在尝试对从 ImageView 中的 url 加载的图像实现滑动效果......我成功了,但现在我想要加载多个 url,我可以通过一个通过在设备屏幕上滑动手指作为覆盖整个屏幕的单个图像...我试了很多 bt 都无法实现..我不知道将使用多个 ImageView ....一些用于循环和数组或什么!只是无奈 b'coz 尝试了 30 多次...

请有人帮忙....提前谢谢......

这是我的代码:

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.webkit.WebView;
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;

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
viewFlipper = new ViewFlipper(this);

ImageView[] img = new ImageView[i];
j_comm_func funs = new j_comm_func();//already made this class
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-1.jpg", bmOptions);
img.setImageBitmap(bm);


viewFlipper.addView(img);



//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;
}
}
}

最佳答案

好吧...您的方法 onTouchEvent 不起作用,因为所有触摸事件都由 ViewFlipper 消耗(从文档到此 Activity 的方法:

Called when a touch screen event was not handled by any of the views under it. This is most useful to process touch events that happen outside of your window bounds, where there is no view to receive it. )

要实现你想要的东西,请看这里:Android touch events with multiple views(such as a ListView in a Viewflliper)

在你的情况下你应该做这样的事情:

img.setOnTouchListener(new DragableViewTouchListener());

要添加缩放功能,您可以做很多事情...我建议您执行以下操作:

  1. 创建ImageView的扩展:ZoomableImageVIew
  2. 像这样重写 dispatchTouchEvent 方法:

    public boolean dispatchTouchEvent(MotionEvent event) {
    if(needZoom(event)) {
    doZoom();
    // the event was handled. Do not propagate it to prevent handling it in ViewFlipper.
    return true;
    } else {
    // propagate the event (to ViewFlipper)
    super.dispatchTouchEvent(event);
    }

祝你好运!

关于android - 向鳍状肢添加多个 ImageView 并将图像从 url 加载到这些 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6468259/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com