网友通过本文主要向大家介绍了android imageview,android圆形imageview,android中imageview,android圆角imageview,android设置imageview等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
Android中使用ImageViewSwitcher实现图片切换轮播导航效果,
前面写过了使用ViewFlipper和ViewPager实现屏幕中视图切换的效果(ViewPager未实现轮播)附链接:
Android中使用ViewFlipper实现屏幕切换
Android中使用ViewPager实现屏幕页面切换和页面切换效果
今天我们在换一种实现方式ImageViewSwitcher。
ImageSwitcher是Android中控制图片展示效果的一个控件,如:幻灯片效果
ImageSwitcher粗略的理解就是ImageView的选择器。
ImageSwitcher的原理:ImageSwitcher有两个子View:ImageView,当左右滑动的时候,就在这两个ImageView之间来回切换来显示图片。
既然有两个子ImageView,那么我们要创建两个ImageView给ImageSwitcher。创建ImageViewSwitcher中的ImageView是通过ViewFactory工厂来实现的。
下面我们展示下本次实现效果(可以轮播哦):
好了,废话不多说,开始撸代码:
第一步:Layout中建立主布局(FrameLayout)文件activity_main.xml(包含导航原点的LinearLayout布局)
1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/activity_main" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.example.administrator.switcher.MainActivity"> 8 <ImageSwitcher 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 android:id="@+id/is"> 12 </ImageSwitcher> 13 <LinearLayout 14 android:id="@+id/point_layout" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:layout_gravity="bottom" 18 android:orientation="horizontal"> 19 <ImageView 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:layout_weight="1" 23 android:src="@mipmap/default_holo"/> 24 <ImageView 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:layout_weight="1" 28 android:src="@mipmap/default_holo"/> 29 <ImageView 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_weight="1" 33 android:src="@mipmap/default_holo"/> 34 <ImageView 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:layout_weight="1" 38 android:src="@mipmap/default_holo"/> 39 </LinearLayout> 40 </FrameLayout>
这里大家也可以通过配置文件来布局下面的导航圆点,不必写死在布局文件中。
第二步:Java中功能实现代码MainActivity.java
1 import android.support.v7.app.AppCompatActivity; 2 import android.os.Bundle; 3 import android.view.MotionEvent; 4 import android.view.View; 5 import android.widget.ImageSwitcher; 6 import android.widget.ImageView; 7 import android.widget.LinearLayout; 8 import android.widget.ViewSwitcher; 9 import java.util.ArrayList; 10 /** 11 * Created by panchengjia on 2016/12/04. 12 */ 13 public class MainActivity extends AppCompatActivity implements ViewSwitcher.ViewFactory,View.OnTouchListener{ 14 private ImageSwitcher is;//声明ImageSwitcher布局 15 private LinearLayout point_layout;//声明导航圆点的布局 16 //图片id数组 17 int[] images={R.mipmap.a1,R.mipmap.a2,R.mipmap.a3,R.mipmap.a4}; 18 //实例化存储导航圆点的集合 19 ArrayList<ImageView> points = new ArrayList<>(); 20 int index;//声明index,记录图片id数组下标 21 float startX;//手指接触屏幕时X的坐标(演示左右滑动) 22 float endX;//手指离开屏幕时的坐标(演示左右滑动) 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 is = (ImageSwitcher) findViewById(R.id.is); 29 is.setFactory(this);//通过工厂实现ImageSwitcher 30 initpoint(); 31 is.setOnTouchListener(this);//设置触摸事件 32 } 33 //初始化导航圆点的方法 34 private void initpoint() { 35 point_layout= (LinearLayout) findViewById(R.id.point_layout); 36 int count = point_layout.getChildCount();//获取布局中圆点数量 37 for(int i =0;i<count;i++){ 38 //将布局中的圆点加入到圆点集合中 39 points.add((ImageView) point_layout.getChildAt(i)); 40 } 41 //设置第一张图片(也就是图片数组的0下标)的圆点状态为触摸实心状态 42 points.get(0).setImageResource(R.mipmap.touched_holo); 43 } 44 //设选中图片对应的导航原点的状态 45 public void setImageBackground(int selectImage) { 46 for(int i=0;i<points.size();i++){ 47 //如果选中图片的下标等于圆点集合中下标的id,则改变圆点状态 48 if(i==selectImage){ 49 points.get(i).setImageResource(R.mipmap.touched_holo); 50 }else{ 51 points.get(i).setImageResource(R.mipmap.default_holo);