当前位置: 代码迷 >> 综合 >> 模拟js的首页动态推荐页面 Gallery 自动播放 无限循环 指示器显示 点击事件
  详细解决方案

模拟js的首页动态推荐页面 Gallery 自动播放 无限循环 指示器显示 点击事件

热度:30   发布时间:2024-01-21 17:07:59.0
//指示器代码
package com.su.testrcmd;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;public class FlowIndicator extends View {private int count;private float space, radius;private int point_normal_color, point_seleted_color;// 选中private int seleted = 0;// background seleted normalpublic FlowIndicator(Context context, AttributeSet attrs) {super(context, attrs);TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.FlowIndicator);count = a.getInteger(R.styleable.FlowIndicator_count, 4);space = a.getDimension(R.styleable.FlowIndicator_space, 9);radius = a.getDimension(R.styleable.FlowIndicator_point_radius, 9);point_normal_color = a.getColor(R.styleable.FlowIndicator_point_normal_color, 0x000000);point_seleted_color = a.getColor(R.styleable.FlowIndicator_point_seleted_color, 0xffff07);int sum = attrs.getAttributeCount();String str = "";for (int i = 0; i < sum; i++) {String name = attrs.getAttributeName(i);String value = attrs.getAttributeValue(i);str += "attr_name :" + name + ": " + value + "\n";}Log.i("attribute", str);a.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++;elseseleted = 0;invalidate();}public void previous() {if (seleted > 0)seleted--;elseseleted = count - 1;invalidate();}@Overrideprotected void onDraw(Canvas canvas) {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);elsepaint.setColor(point_normal_color);canvas.drawCircle(width + getPaddingLeft() + radius + i* (space + radius + radius), getHeight() / 2, radius, paint);}}@Overrideprotected 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;}}
package com.su.testrcmd;
//gallery的适配器
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;public class GalleryAdapter extends BaseAdapter {Context mContext;int[] res = new int[] { R.drawable.t1, R.drawable.t2, R.drawable.t3 };public GalleryAdapter(Context cnt) {this.mContext = cnt;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn Integer.MAX_VALUE;}public int getRealCount() {// TODO Auto-generated method stubreturn res.length;}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int arg0) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {// TODO Auto-generated method stubif (arg1 == null) {arg1 = LayoutInflater.from(mContext).inflate(R.layout.gallery_item,null);}ImageView img = (ImageView) arg1.findViewById(R.id.home_img);img.setImageResource(res[arg0 % res.length]);return arg1;}
}

package com.su.testrcmd;
//主界面
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;public class TestRcmdActivity extends Activity {static final int SCROLL_ACTION = 0;private Gallery mGallery;private com.su.testrcmd.FlowIndicator mMyView;private com.su.testrcmd.GalleryAdapter mGalleryAdapter;private Timer mTimer;private int realcount;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mTimer = new Timer();mTimer.scheduleAtFixedRate(new MyTask(), 0, 5000);mGallery = (Gallery) findViewById(R.id.home_gallery);mMyView = (FlowIndicator) findViewById(R.id.myView);mGalleryAdapter = new GalleryAdapter(this);realcount = mGalleryAdapter.getRealCount();mMyView.setCount(realcount);mGallery.setAdapter(mGalleryAdapter);mGallery.setSelection(200);mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stubmMyView.setSeletion(arg2 % realcount);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {// TODO Auto-generated method stub}});mGallery.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {Toast.makeText(TestRcmdActivity.this, "测试" + arg2 % realcount,10).show();}});}private class MyTask extends TimerTask {@Overridepublic void run() {mHandler.sendEmptyMessage(SCROLL_ACTION);}}Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.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;}}};
}


主界面xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res/com.su.testrcmd"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><Galleryandroid:id="@+id/home_gallery"android:layout_width="fill_parent"android:layout_height="wrap_content"android:spacing="5dip" /><LinearLayoutandroid:id="@+id/linearLayout1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="#65000000"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginBottom="5dip"android:layout_marginTop="5dip"android:text="推荐图片页面测试"android:textColor="#ffffff"android:textSize="18dip" /><com.su.testrcmd.FlowIndicatorandroid:id="@+id/myView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginBottom="5dip"app:count="4"android:gravity="center"android:paddingRight="20dip"app:point_normal_color="#45000000"app:point_radius="3dip"app:point_seleted_color="#ffffff"app:point_size="5dip"app:space="10dip" /></LinearLayout></FrameLayout>


 

item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><ImageViewandroid:id="@+id/home_img"android:layout_width="wrap_content"android:layout_height="wrap_content"android:adjustViewBounds="true" android:src="@drawable/t1"/></FrameLayout>
指示器的风格文件 
<?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>