Android中的ImageSwitch控件

    xiaoxiao2023-03-24  7

    ImageSwitch是Android中又一个图片查看小组件,说它是小组件是因为我不太会用,也不怎么常用,它改变显示图片的方式要通过其他控件,整个显示过程都要手动操控,相同的功能Gallery和AdapterViewFlipper能够很好的完成,重要的是ImageSwitch只能显示图片。 下面做一个简单的实现,整三张图片试试。布局文件中只需要写入ImageSwitch标签即可,并为它准备两个按钮控制图片变化。之后的工作就在Java代码中完成,首先设定工厂,什么意思呢,就是一个造图片的,ImageSwitch切换图片的机制就是通过setImageResource()方法设定当前显示的图片,而这个图片需要经过工厂承接才能放到ImageSwitch上,然后通过一定的逻辑控制就可以切换图片了。 布局文件如下 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.briup.imageswitch.MainActivity" > <ImageSwitcher android:id="@+id/imageswitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="btn1" android:text="上一张" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="btn2" android:text="下一张" /> </LinearLayout> </RelativeLayout>

    MainActivity.class类文件如下

    import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.animation.AnimationUtils; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity { private ImageSwitcher imageswitch; private int position = 0;//当前显示图片位置 int[] images = {R.drawable.chihuahua_bone,R.drawable.cocktail,R.drawable.corgi};//图片资源数组 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); imageswitch = (ImageSwitcher) findViewById(R.id.imageswitch); imageswitch.setFactory(new ViewFactory() {//设定工厂,每进来一个图片都用一个ImageView接收 @Override public View makeView() { return new ImageView(MainActivity.this); } }); imageswitch.setImageResource(images[position]);//设置初始图片 imageswitch.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//动画淡入 imageswitch.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//动画淡出 } public void btn1(View v){ if(position==0)//上一张到第一张时把位置设到最后一张 position = images.length; imageswitch.setImageResource(images[--position]); } public void btn2(View v){ if(position==images.length-1)//下一张到最后一张时把位置设到第一张 position = -1; imageswitch.setImageResource(images[++position]); } }

    效果如下

    转载请注明原文地址: https://ju.6miu.com/read-1201069.html
    最新回复(0)