Android制作一个图片定时轮播效果,如下图所示,小圆圈与图片一一对应,有多少张图片就有多少个小圆圈,当前图片所对应的小圆圈为红色。
在下不才,在这道题中仅仅是考虑到了计时操作,单击事件处理和绝对布局。
其实,图片播放器的制作相信我们大家都会的,使用一个ImageSwitcher图片切换器即可,但是怎么在图片上增加小圆圈呢?这问题还是有点意思的,在我的代码里面采用了绝对布局,虽然这个已经是不建议使用的布局,但仍能完成本题的目标。
话不多说,贴代码!!!!
//MainActivity.java
package com.example.dell.ch4_11;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
public class MainActivity extends AppCompatActivity {
private ImageSwitcher mSwitcher;
private int[] imgIds = new int []{
R.mipmap.pic1,R.mipmap.pic2,R.mipmap.pic3,
R.mipmap.pic4
};
private int[] btnIds = new int[]{
R.id.btn1,R.id.btn2,R.id.btn3,R.id.btn4
};
private ImageView[] imageView = new ImageView[4];
private int currentIndex = 0;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwitcher = (ImageSwitcher) findViewById(R.id.mSwitcher);
mSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView mImageView = new ImageView(MainActivity.this);
return mImageView;
}
});
mSwitcher.setImageResource(imgIds[currentIndex]);
for(int i=0;i<imageView.length;i++){
imageView[i] = findViewById(btnIds[i]);
}
mHandler = new Handler(){
public void handleMessage(Message msg){
if(msg.what == 0x11){
currentIndex = (currentIndex+1) % btnIds.length;
mSwitcher.setImageResource(imgIds[currentIndex]);
for(int i=0;i<imageView.length;i++){
imageView[i].setImageResource(R.mipmap.btn2);
}
imageView[currentIndex%imageView.length].setImageResource(R.mipmap.btn1);
}
}
};
start();
}
public void start(){
new Thread(){
public void run(){
while(true){
try{
Thread.sleep(3000);
mHandler.sendEmptyMessage(0x11);
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
}.start();
}
}
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<AbsoluteLayout
android:layout_width="240dp"
android:layout_height="150dp">
<ImageSwitcher
android:id="@+id/mSwitcher"
android:layout_width="240dp"
android:layout_height="150dp"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right"
/>
<ImageView
android:id="@+id/btn1"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/btn1"
android:layout_x="40dp"
android:layout_y="110dp"
/>
<ImageView
android:id="@+id/btn2"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/btn2"
android:layout_x="80dp"
android:layout_y="110dp"
/>
<ImageView
android:id="@+id/btn3"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/btn2"
android:layout_x="120dp"
android:layout_y="110dp"
/>
<ImageView
android:id="@+id/btn4"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/btn2"
android:layout_x="160dp"
android:layout_y="110dp"
/>
</AbsoluteLayout>
</LinearLayout>
界面效果: