FlowIndicator 自定义左右图片切换
一、效果图:
二、布局文件
header_view.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app1="http://schemas.android.com/apk/res/com.johnny.flowindicatortest" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Gallery android:id="@+id/home_gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:spacing="5dp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="#65000000" android:orientation="vertical" > <TextView android:id="@+id/tv_gal_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:textColor="#ffffff" android:textSize="18sp" /> <com.johnny.flowindicatortest.FlowIndicator android:id="@+id/myview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dip" android:gravity="center" app:count="4" app:point_normal_color="#45000000" app:point_radius="3dip" app:point_seleted_color="#ffffff" app:point_size="5dip" app:space="10dp" /> </LinearLayout> </FrameLayout>
gallery_item.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/home_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:adjustViewBounds="true" android:src="@drawable/t1" /> </FrameLayout>
一个资源文件
attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="FlowIndicator"> <attr name="count" format="integer" /> <attr name="space" format="dimension" /> <attr name="point_size" format="dimension" /> <attr name="point_seleted_color" format="color|reference" /> <attr name="point_normal_color" format="color|reference" /> <attr name="point_radius" format="dimension" /> </declare-styleable> </resources>FlowIndicator.java
public class FlowIndicator extends View {
private int count;
private float space, radius;
private int point_normal_color, point_seleted_color;
// 选中
private int seleted = 0;
public FlowIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
//提供TypedArray(用于Drawable对象数组)的XML资源。
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.FlowIndicator);
count = typedArray.getInteger(R.styleable.FlowIndicator_count,4);
space = typedArray.getDimension(R.styleable.FlowIndicator_space, 9);
radius = typedArray.getDimension(R.styleable.FlowIndicator_point_radius, 9);
point_normal_color = typedArray.getColor(R.styleable.FlowIndicator_point_normal_color, 0x000000);
point_seleted_color = typedArray.getColor(R.styleable.FlowIndicator_point_seleted_color, 0xffff07);
int sum = attrs.getAttributeCount();
if(Constans.DEBUG){
String str = "";
for(int i=0;i<sum;i++){
String name = attrs.getAttributeName(i);
String value = attrs.getAttributeValue(i);
str += "sttr_name:" + name +": " + value +"\n";
}
Log.i("attribute", str);
}
typedArray.recycle();
}
public void setSeletion(int index){
this.seleted = index;
//重绘
invalidate();
}
public void setCount(int count){
this.count = count;
invalidate();
}
public void next(){
if(seleted < count-1){
seleted++;
}else{
seleted = 0;
}
invalidate();
}
public void previous(){
if(seleted > 0 ){
seleted--;
}else{
seleted = count-1;
}
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);
float width = (getWidth() - ((radius * 2 * count) + (space * (count - 1))))/2.f;
for (int i = 0; i < count; i++) {
if (i == seleted)
paint.setColor(point_seleted_color);
else
paint.setColor(point_normal_color);
canvas.drawCircle(width + getPaddingLeft() + radius + i
* (space + radius + radius), getHeight() / 2, radius, paint);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = (int) (getPaddingLeft() + getPaddingRight()
+ (count * 2 * radius) + (count - 1) * radius + 1);
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = (int) (2 * radius + getPaddingTop() + getPaddingBottom() + 1);
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
}
MainActivity.java
public class MainActivity extends Activity {
private static final int SCROLL_ACTION = 0;
private TextView textView;
private Gallery mGallery;
private FlowIndicator myView;
Timer mTimer;
private GalleryAdapter galleryAdapter;
private String[] titles = {"标题1","标题2","标题3","标题4","标题5","标题6","标题7","标题8","标题9"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.header_view);
viewInit();
//定时滚动
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new MyTask(), 0, 5000);
}
private void viewInit(){
textView = (TextView) findViewById(R.id.tv_gal_title);
mGallery = (Gallery) findViewById(R.id.home_gallery);
myView = (FlowIndicator) findViewById(R.id.myview);
galleryAdapter = new GalleryAdapter(this);
myView.setCount(galleryAdapter.getCount());
mGallery.setAdapter(galleryAdapter);
mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
textView.setText(titles[arg2]);
myView.setSeletion(arg2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
private class GalleryAdapter extends BaseAdapter{
Context mContext;
int[] res = new int[] { R.drawable.t1, R.drawable.t2,
R.drawable.t3, R.drawable.t1, R.drawable.t2,
R.drawable.t3, R.drawable.t1, R.drawable.t2,
R.drawable.t3 };
public GalleryAdapter(Context cnt) {
this.mContext = cnt;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return res.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return res[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.gallery_item, null);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.home_img);
imageView.setImageResource(res[position]);
return convertView;
}
}
private class MyTask extends TimerTask {
@Override
public void run() {
mHandler.sendEmptyMessage(SCROLL_ACTION);
}
}
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case SCROLL_ACTION:
MotionEvent e1 = MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
89.333336f, 265.33334f, 0);
MotionEvent e2 = MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
300.0f, 238.00003f, 0);
mGallery.onFling(e1, e2, -1300, 0);
break;
default:
break;
}
}
};
}
Constans.java
public class Constans {
public static boolean DEBUG = true;
}