Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Autoscroll imageview_Android_Android Imageview_Android Scrollview - Fatal编程技术网

Android Autoscroll imageview

Android Autoscroll imageview,android,android-imageview,android-scrollview,Android,Android Imageview,Android Scrollview,在我的活动中,我只有一个ImageView。在它里面,src是一张比屏幕大得多的图片。我希望图片从左向右缓慢滚动,直到到达照片的右边距,然后开始向左滚动,直到达到左边距。然后重新开始。 我需要它发生在一个单独的线程,使手机不会冻结,而这发生了。 我怎样才能做到这一点? 是否有默认情况下执行此操作的小部件 更新代码 //布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http:/

在我的活动中,我只有一个ImageView。在它里面,src是一张比屏幕大得多的图片。我希望图片从左向右缓慢滚动,直到到达照片的右边距,然后开始向左滚动,直到达到左边距。然后重新开始。 我需要它发生在一个单独的线程,使手机不会冻结,而这发生了。

我怎样才能做到这一点? 是否有默认情况下执行此操作的小部件

更新代码 //布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mylinear"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="center"
        android:src="@drawable/rainforest" />

</RelativeLayout>
但它与图片不符。 我的意思是,卷轴从左到右,将图片推到左边,并在ImageView下面显示白色背景

  • 还有,这应该是在一条线里面还是什么的?我需要能够以某种方式退出这个“滚动”活动,而不必使用后退按钮。我希望在ImageView的顶部有一个按钮(按钮应该保持静止),单击它会启动另一个意图

  • 看起来ImageView中的图片被裁剪以适合屏幕内部。我怎样才能克服这个问题


您可以在ScrollView中添加ImageView。像这样

<ScrollView
    android:id="@+id/img_scroll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/IMAGENAME" />
</ScrollView>

当您想要自动滚动图像时,您可以创建一个AsyncTask和内部doInBackground()方法,创建一个
new Runnable
RunnuIthread()
,并使用它的ID向ScrollView发送命令,以便根据需要进行滚动。

示例:

private Animation           _translateAnimation;
private ImageView       _image;

_image = (ImageView)findViewById(R.id.yourimage);

_translateAnimation = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, 100f, TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, 0f);
_translateAnimation.setDuration(10000);
_translateAnimation.setRepeatCount(-1);
_translateAnimation.setRepeatMode(Animation.REVERSE);
_translateAnimation.setInterpolator(new LinearInterpolator());
_image.setAnimation(_translateAnimation);

这将使_linearLoading从右到左、从右到左缓慢转换

持续时间越长=速度越慢

translateImation.ABSOLUTE=使用图像的位置 TranslateAnimation.RELATIVE_TO_PARENT=使用父布局

方法TranslateImation(开始x、结束x、开始y、结束y)


因此,我的问题的解决方案就在这里(对于所有像我这样的新安卓开发者来说,他们的应用可能需要这个):

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/global_relative"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="600dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@drawable/rainforest"
        android:scaleType="center"
        android:src="@drawable/rainforest613x490" >

    </ImageView>
</ScrollView>  
</RelativeLayout>

只需确保您的图像足够大/高以适合屏幕,并且(+300dp)比屏幕宽度宽(或调整上面的代码)


快乐编码;)

你应该使用TranslateAnimation和一些重复属性,所以使用图像的动画有点不稳定…请检查我的问题,查看使用TranslateAnimation更新的代码是否无效…\u linearLoading应该是你想要翻译的,这是你的图像。因此,您必须使用findviewbyid()获取它…我认为我的错误是我设置了imageview的动画。。。imageview似乎将实际图像裁剪为可见大小,因为无论我在哪里进行翻译,都会出现白色背景,而不是显示更多的源图片。为了看到图片的其余部分而不是背景,我应该怎么做?我使用这段代码在加载屏幕中翻译带有背景图像的线性布局,它工作得非常好。如果使用linearLayout而不是ImageView,则应用程序会崩溃。你能把你使用的和适合你的全部动画代码放在这里吗?这对我不起作用。它崩溃了。你能给我一些关于如何向scrollview发送命令的提示代码吗?太棒了!为我工作。嘿,我也面临同样的问题。你能帮我解决这个问题吗。。。在过去的3个小时里,我一直在努力解决这个问题。。。。谢谢滚动视图不是必需的,关键是将自定义宽度设置为ImageView(600dp),然后设置缩放类型中心,然后将平移动画添加到ImageView。
public class MainActivity extends Activity {

    Animation           _translateAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        _translateAnimation = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, -300f, TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, 0f);
        _translateAnimation.setDuration(8000);
        _translateAnimation.setRepeatCount(-1);
        _translateAnimation.setRepeatMode(Animation.REVERSE); // REVERSE
        _translateAnimation.setInterpolator(new LinearInterpolator());
        ImageView img = (ImageView) findViewById(R.id.img);
        img.startAnimation(_translateAnimation);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/global_relative"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="600dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@drawable/rainforest"
        android:scaleType="center"
        android:src="@drawable/rainforest613x490" >

    </ImageView>
</ScrollView>  
</RelativeLayout>