图片的自动轮播
package com.baway.test;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import com.baway.bean.ImageShow;
import com.baway.bean.SuperXq;
import com.baway.myutils.MyUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.BitmapUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
/*
* 设置详情页面
* */
public class ShowActivity extends Activity {
private String url;
private String id;
private List<ImageShow> list_i;
private List<ImageView> list;
private ImageView imageView;
private ViewPager vp;
private LinearLayout line;
private List<View> list2;
int count = 0;
int count1 = 0;
int oldindex = 0;
// 创建handler
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
vp.setCurrentItem(msg.what);
System.out.println(msg.what+"+++++"+list2.size());
count1 = msg.what;
list2.get(count1).setBackgroundResource(R.drawable.dot_focus);
list2.get(oldindex).setBackgroundResource(R.drawable.dot_nomal);
oldindex = count1;
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
// 设置详情的路径
url = "http://mobile.hmeili.com/yunifang/mobile/goods/detail";
// 接收值
Intent intent = getIntent();
id = intent.getStringExtra("id");
new MyAsyncTask().execute();
// 找到控件
vp = (ViewPager) findViewById(R.id.vp);
line = (LinearLayout) findViewById(R.id.line);
}
private Timer timer;
class MyAsyncTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String str = MyUtils.Check_j(url + "?" + "id" + "=" + id);
return str;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Gson gson = new Gson();
SuperXq sx = gson.fromJson(result, SuperXq.class);
String list1 = sx.getData().getGoods().getGoods_desc();
Type type = new TypeToken<List<ImageShow>>() {
}.getType();
Gson g = new Gson();
list_i = g.fromJson(list1, type);
getImageList();
getdotList();
list2.get(0).setBackgroundResource(R.drawable.dot_focus);
vp.setAdapter(new MyAdapter());
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (count == list_i.size()) {
count = 0;
}
handler.sendEmptyMessage(count);
count++;
}
}, 0, 1500);
super.onPostExecute(result);
}
}
class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(list.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(list.get(position));
return list.get(position);
}
}
private void getImageList() {
// 创建一个集合
list = new ArrayList<ImageView>();
BitmapUtils bitmapUtils = new BitmapUtils(this);
for (int i = 0; i < list_i.size(); i++) {
ImageView imView = new ImageView(this);
bitmapUtils.display(imView, list_i.get(i).getUrl());
list.add(imView);
}
}
private void getdotList() {
// 创建一个集合
list2 = new ArrayList<View>();
for (int i = 0; i < list_i.size(); i++) {
View view = View.inflate(ShowActivity.this, R.layout.dot_layout,
null);
View view1 = view.findViewById(R.id.dotView);
list2.add(view1);
line.addView(view);
}
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
if (timer!=null) {
timer.cancel();
}
super.onStop();
}
}
/////////////////////////////////////////////////////////////activity_show//////////////////////////////////////////////////////////////////////////////////////
<RelativeLayout 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"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
></android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
></LinearLayout>
</RelativeLayout>
////////////////////////////////////////////////////////////////////dotlayout//////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:id="@+id/dotView"
android:layout_width="8dp"
android:layout_height="8dp"
android:layout_margin="10dp"
android:background="@drawable/dot_nomal"
/>
</LinearLayout>
//////////////////////////////////////////////////////////////////dot_fauces.xml//////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ff0000"/>
<corners android:radius="8dip"/>
</shape>
//////////////////////////////////////////////////////////////////dot_normal.xml//////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#0f0"/>
<corners android:radius="8dip"/>
</shape>