指示图片滑动标点View设置
package com.dream.myqiyi.widget;import com.dream.myqiyi.Constans;import com.dream.myqiyi.R;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 normal public 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(); if (Constans.DEBUG) { 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++; else seleted = 0; invalidate(); } public void previous() { if (seleted > 0) seleted--; else seleted = count - 1; invalidate(); } @Override protected 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); 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; }}
Gallery适配器设置
package com.dream.myqiyi.home;
import com.dream.myqiyi.R;
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, 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 null; }@Override
public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; }@Override
public View getView(int arg0, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub if (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]); return arg1; } }首页代码
package com.dream.myqiyi.home;
import java.util.Timer;
import java.util.TimerTask; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;import com.dream.myqiyi.R;
import com.dream.myqiyi.widget.FlowIndicator;import android.app.Activity;
import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.Gallery; import android.widget.ImageView; import android.widget.ExpandableListView.OnGroupCollapseListener; import android.widget.ExpandableListView.OnGroupExpandListener; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast;public class HomeActivity extends Activity {
static final int SCROLL_ACTION = 0; private static Boolean isExit = false; private static Boolean hasTask = false; ExpandableListView mExpandableListView; int[] tags = new int[] { 0, 0, 0, 0, 0 }; String[] groups = new String[] { "同步剧场", "奇艺出品", "热播电影", "3月片花速递", "动漫乐园" }; String[][] childs = new String[5][10]; Gallery mGallery; GalleryAdapter mGalleryAdapter; FlowIndicator mMyView; private ExecutorService executorService; private Handler handler; private ProgressDialog progressDialog; //按2次返回键退出事件 Timer mTimer; Timer tExit = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub isExit = false; hasTask = true; } }; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK) { if (isExit == false) { isExit = true; Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show(); if (!hasTask) { tExit.schedule(task, 2000); } } else { finish(); System.exit(0); } } return false; } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.home_activity); ononein(); } private void ononein() { // TODO Auto-generated method stub progressDialog = new ProgressDialog(getParent());// 生成一个进度条 progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.setTitle("请稍等"); progressDialog.setMessage("正在读取数据中..."); //建立线程异步加载数据 executorService = Executors.newFixedThreadPool(4); handler = new GetHomeTimeLineHandler(); executorService.submit(new GetHomeTimeLineThread()); progressDialog.show(); } private class GetHomeTimeLineHandler extends Handler { public void handleMessage(Message msg) { prepareView(); mTimer = new Timer(); //创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后在initialDelay+period 后执行,接着在 mTimer.scheduleAtFixedRate(new MyTask(), 0, 5000); progressDialog.dismiss();//关闭进度条 } } private class GetHomeTimeLineThread implements Runnable{@Override
public void run() { // TODO Auto-generated method stub try { Thread.sleep(2000); Message msg = handler.obtainMessage();// 通知线程来处理范围的数据 msg.sendToTarget(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }private void prepareView() {
mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView1); MyListAdapter adapter = new MyListAdapter(); View header = LayoutInflater.from(this).inflate(R.layout.header_view, null); mGallery = (Gallery) header.findViewById(R.id.home_gallery); mMyView = (FlowIndicator) header.findViewById(R.id.myView); mGalleryAdapter = new GalleryAdapter(this); mMyView.setCount(mGalleryAdapter.getCount()); mGallery.setAdapter(mGalleryAdapter); mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub mMyView.setSeletion(arg2); }@Override
public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); mGallery.setOnItemClickListener(new OnItemClickListener() {@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub //Toast.makeText(HomeActivity.this, String.valueOf(arg2), 300).show(); Intent intent = new Intent(HomeActivity.this, ProimageActivity.class); startActivity(intent); } }); mExpandableListView.addHeaderView(header); mExpandableListView.setAdapter(adapter); mExpandableListView .setOnGroupExpandListener(new OnGroupExpandListener() {@Override
public void onGroupExpand(int arg0) { // TODO Auto-generated method stub tags[arg0] = 1; } }); mExpandableListView .setOnGroupCollapseListener(new OnGroupCollapseListener() {@Override
public void onGroupCollapse(int arg0) { // TODO Auto-generated method stub tags[arg0] = 0; } }); }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: // int curPos = mGallery.getSelectedItemPosition(); // if (curPos < mGalleryAdapter.getCount() - 1) { // curPos++; // } else { // curPos = 0; // } // // mGallery.setLayoutAnimation(new LayoutAnimationController( // // AnimationUtils.loadAnimation(HomeActivity.this, // // R.anim.gallery_in))); // mGallery.setSelection(curPos, true); 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; } } };class MyListAdapter extends BaseExpandableListAdapter {
class GroupHolder {
ImageView img,img2; TextView title; LinearLayout linlayout; } public MyListAdapter() { for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { childs[i][j] = "child" + i + "_" + j; } } }@Override
public String getChild(int arg0, int arg1) { // TODO Auto-generated method stub return childs[arg0][arg1]; }@Override
public long getChildId(int arg0, int arg1) { // TODO Auto-generated method stub return 0; }@Override
public View getChildView(final int arg0,final int arg1, boolean arg2, View arg3, ViewGroup arg4) { // TODO Auto-generated method stub GroupHolder groupHolder; if (arg3 == null) { groupHolder = new GroupHolder(); arg3 = LayoutInflater.from(HomeActivity.this).inflate( R.layout.list_child_item, null); groupHolder.linlayout = (LinearLayout)arg3.findViewById(R.id.linearLayout1); groupHolder.img2 = (ImageView)arg3.findViewById(R.id.imageView2); groupHolder.linlayout.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //Toast.makeText(HomeActivity.this, String.valueOf(arg0)+"-"+String.valueOf(arg1), 300).show(); Intent htwo = new Intent(HomeActivity.this, HometwoActivity.class); htwo.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); View vi = HomeGroup.group.getLocalActivityManager().startActivity("HometwoActivity", htwo).getDecorView(); HomeGroup.group.replaceView(vi); } }); groupHolder.img2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //Toast.makeText(HomeActivity.this, String.valueOf(arg0)+"-"+String.valueOf(arg1), 300).show(); Intent htwoo = new Intent(HomeActivity.this, HometwoActivity.class); htwoo.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); View vii = HomeGroup.group.getLocalActivityManager().startActivity("HometwoActivity", htwoo).getDecorView(); HomeGroup.group.replaceView(vii); } }); } return arg3; }@Override
public int getChildrenCount(int arg0) { // TODO Auto-generated method stub return 10; }@Override
public Object getGroup(int arg0) { // TODO Auto-generated method stub return groups[arg0]; }@Override
public int getGroupCount() { // TODO Auto-generated method stub return groups.length; }@Override
public long getGroupId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) { // TODO Auto-generated method stub GroupHolder groupHolder; if (arg2 == null) { arg2 = LayoutInflater.from(HomeActivity.this).inflate( R.layout.list_group_item, null); groupHolder = new GroupHolder(); groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img); groupHolder.title = (TextView) arg2 .findViewById(R.id.title_view); arg2.setTag(groupHolder); } else { groupHolder = (GroupHolder) arg2.getTag(); } if (tags[arg0] == 0) { groupHolder.img .setImageResource(R.drawable.list_indecator_button); } else { groupHolder.img .setImageResource(R.drawable.list_indecator_button_down); } groupHolder.title.setText(groups[arg0]); return arg2; }@Override
public boolean hasStableIds() { // TODO Auto-generated method stub return true; }@Override
public boolean isChildSelectable(int arg0, int arg1) { // TODO Auto-generated method stub return true; }}
}