Android(安卓)Studio基础学习(入门五)——小相册(三)【ImageSwitcher图像切换器】
16lz
2021-01-25
基础UI控件:
按钮,编辑框,单选按钮,复选框,计时器,日期选择器,时间选择器,图片按钮,文本框。
Button,EditText,RadioButton,CheckBox,Chronometer,DatePicker,TimePicker,ImageButton,TextView。
图像切换器:ImageSwitcher。
Android基础学习,话不多说,上代码:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
MainActivity.java:
package com.zzx.showpictures;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.WindowManager;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher;public class MainActivity extends AppCompatActivity { private int[] arrayPicture=new int[]{R.mipmap.img01,R.mipmap.img02,R.mipmap.img03, R.mipmap.img04,R.mipmap.img05,R.mipmap.img06, R.mipmap.img07,R.mipmap.img08,R.mipmap.img09}; private ImageSwitcher imageSwitcher; private int index; private float touchDownX; private float touchUpX; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //设置全屏显示 imageSwitcher=findViewById(R.id.imageswitcher); imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { ImageView imageView=new ImageView(MainActivity.this); imageView.setImageResource(arrayPicture[index]); return imageView; } }); imageSwitcher.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_DOWN){ touchDownX=event.getX(); return true; }else if(event.getAction()==MotionEvent.ACTION_UP){ touchUpX=event.getX(); if(touchUpX-touchDownX>100){ //判断为从左向右滑动 index=index==0?arrayPicture.length-1:index-1; //动画资源 imageSwitcher.setImageResource(arrayPicture[index]); } else if(touchDownX-touchUpX>100){ //判断为从左向右滑动 index=index==arrayPicture.length-1?0:index+1; //动画资源 imageSwitcher.setImageResource(arrayPicture[index]); } return true; } return false; } }); }}
图片资源随意放上几个即可,建议使用png。
演示效果,因为没有添加动画资源,所以只能看出来切换,并没有看出来切换过程:
我是用alt+左键,在左右两边点击的时候,实现了切换,或者使用从左向右滑动和从右向左滑动也可以实现切换:
更多相关文章
- 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
- 一款霸榜 GitHub 的开源 Linux 资源监视器!
- android标题栏中添加返回按钮
- android 实现男女按钮选择
- Android开发者必备的14个工具资源
- android 触发Button按钮事件的三种方式
- Android(安卓)ANR 分析学习总结(转)
- android动画之通过子线程来实现动画
- OPhone 2.0的2D动画实践(三)