好久没有更博了,最近放假休息了一段时间
今天为大家分享出Gallery画廊来实现图片切换
package com.example.a13_d01;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
// 画廊
private Gallery gl_main_gl;
// 自定义适配器
private MyBaseAdapter myBaseAdapter;
// 图片资源
private int images[] = new int[] { R.drawable.f1, R.drawable.f2,
R.drawable.f3, R.drawable.f4, R.drawable.f5, R.drawable.f6 };
// 原点控件资源
private ImageView imagesIds[] = new ImageView[6];
// 当前的图片的下标
private int selectIndex = 0;
// 线程的初始化时间
private int times = 0;
// Handler修改控件
private Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
// 当前图片下标加一
gl_main_gl.setSelection(selectIndex + 1);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到控件的R.id资源
findView();
// 1.数据
// 2.创建适配器
myBaseAdapter = new MyBaseAdapter();
// 3.绑定适配器
gl_main_gl.setAdapter(myBaseAdapter);
// 添加图片被选中事件
gl_main_gl
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View arg1, int position, long id) {
// 每次选中新的图片线程时间清零
times = 0;
// 取出选中的图片下标
int index = position % images.length;
// 如果与之前存储的图片下标不一致则说明需要改动小圆点
if (selectIndex != index) {
imagesIds[index]
.setImageResource(R.drawable.ic_dot_focused);
imagesIds[selectIndex]
.setImageResource(R.drawable.ic_dot_normal);
// 将原先的记录图片的下标进行改动
selectIndex = index;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// 利用安卓里面的线程来创建线程一样能够达到线程的实现效果
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
times += 100;// 时间加加
if (times >= 3000) {// 显示3秒之后改动控件 (即换成下一个控件)
myHandler.sendEmptyMessage(1);
}
}
}, 100, 100);
}
// 自定义适配器
class MyBaseAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position % images.length;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position % images.length;
}
@SuppressWarnings("deprecation")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 图片需要放在布局当中
RelativeLayout relativeLayout = new RelativeLayout(
MainActivity.this);
// 设置参数(宽高)
relativeLayout.setLayoutParams(new Gallery.LayoutParams(
Gallery.LayoutParams.MATCH_PARENT,
Gallery.LayoutParams.MATCH_PARENT));
// 背景换成了先前做好的边框图
relativeLayout.setBackgroundResource(R.drawable.bg_gallery);
// 设置内边距
relativeLayout.setPadding(2, 2, 2, 2);
// 图片控件
ImageView imageView = new ImageView(MainActivity.this);
// 设置参数(宽高)
imageView.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT));
// 图片进行填充
imageView.setScaleType(ScaleType.FIT_XY);
// 更换图片资源
imageView.setImageResource(images[position % images.length]);
// 将图片空间添加到布局当中
relativeLayout.addView(imageView);
// 返回该布局
return relativeLayout;
}
}
private void findView() {
gl_main_gl = (Gallery) findViewById(R.id.gl_main_gl);
imagesIds[0] = (ImageView) findViewById(R.id.iv_main_iv1);
imagesIds[1] = (ImageView) findViewById(R.id.iv_main_iv2);
imagesIds[2] = (ImageView) findViewById(R.id.iv_main_iv3);
imagesIds[3] = (ImageView) findViewById(R.id.iv_main_iv4);
imagesIds[4] = (ImageView) findViewById(R.id.iv_main_iv5);
imagesIds[5] = (ImageView) findViewById(R.id.iv_main_iv6);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
xml的代码如下:
<LinearLayout 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:orientation="vertical"
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=".MainActivity" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="120dp" >
<Gallery
android:id="@+id/gl_main_gl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="none" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_gravity="bottom"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#80776f63" >
<ImageView
android:id="@+id/iv_main_iv6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="10dp"
android:src="@drawable/ic_dot_normal" />
<ImageView
android:id="@+id/iv_main_iv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/iv_main_iv6"
android:paddingRight="10dp"
android:src="@drawable/ic_dot_normal" />
<ImageView
android:id="@+id/iv_main_iv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/iv_main_iv5"
android:paddingRight="10dp"
android:src="@drawable/ic_dot_normal" />
<ImageView
android:id="@+id/iv_main_iv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/iv_main_iv4"
android:paddingRight="10dp"
android:src="@drawable/ic_dot_normal" />
<ImageView
android:id="@+id/iv_main_iv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/iv_main_iv3"
android:paddingRight="10dp"
android:src="@drawable/ic_dot_normal" />
<ImageView
android:id="@+id/iv_main_iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/iv_main_iv2"
android:paddingRight="10dp"
android:src="@drawable/ic_dot_focused" />
</RelativeLayout>
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="循环播放外加小圆点显示" />
</LinearLayout>
还有一种是使用ViewPager来实现这个功能
不过两者各有优点和缺点
ViewPager不能进行无限循环播放
但是它可以自带标题
具体使用方便据需求来就可以了。