實現圖片輪播,並且帶圓點 ,輪播時 圓點改變顏色;


package com.example.day14;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import com.lidroid.xutils.BitmapUtils;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
int count = 0;
int olddotindex = 0;
//圖片數據
private String[] image_url = new String[]{
"http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg",
"http://img3.3lian.com/2013/s1/20/d/57.jpg",
"http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg",
"http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg"};
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {

pager.setCurrentItem(count);
dots.get(olddotindex).setBackgroundResource(R.drawable.dot_nomal);
dots.get(count).setBackgroundResource(R.drawable.dot_focus);
olddotindex=count;


};
};
private ArrayList<View> dots;
private LinearLayout la;
private ArrayList<ImageView> images;
private ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

init();

}
private void init() {
// TODO Auto-generated method stub
pager = (ViewPager) findViewById(R.id.viewpager);
la = (LinearLayout) findViewById(R.id.yuandian);
//獲得網絡圖片,配置給Veiwpager
getImageViewList();
//獲得圓點
getDotList();
//設置第一個圓點為選中狀態
dots.get(0).setBackgroundResource(R.drawable.dot_focus);
pager.setAdapter(new myvpAdapter());
//通過定時器,制作自動划動效果
Timer timer=new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
// TODO Auto-generated method stub
count++;
if (count==4) {
count=0;
}

handler.sendEmptyMessage(count);
}
}, 3000, 2000);
}
//獲得圓點
private void getDotList() {
dots = new ArrayList<View>();
//循環圖片數組,創建對應數量的dot
for (int i = 0; i < image_url.length; i++) {
View view=LayoutInflater.from(this).inflate(R.layout.dot, null);//加載布局;
//得到布局中的dot點組件
View dot=view.findViewById(R.id.dotview);
//收集dot;
dots.add(dot);
//把布局添加到線性布局
la.addView(view);
}
}
//獲得網絡圖片,配置給Veiwpager
private void getImageViewList() {
images = new ArrayList<ImageView>();
BitmapUtils bitmapUtils=new BitmapUtils(this);
//加載圖片;
for (int i = 0; i < image_url.length; i++) {
ImageView imageView=new ImageView(this);
imageView.setScaleType(ScaleType.FIT_XY);
bitmapUtils.display(imageView, image_url[i]);
images.add(imageView);
}



}
class myvpAdapter extends PagerAdapter{

@Override
public int getCount() {
// TODO Auto-generated method stub
return images.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) {
// TODO Auto-generated method stub

ImageView im=images.get(position);
container.removeView(im);

}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(images.get(position));
return images.get(position);


}
}
}

 在res下建立一個drawable文件夾;

在drawable里面創建2個xml文件;

一個是dot_focus.xml;

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#f00"/>
<corners android:radius="8dip"/>

</shape>

一個是dot_nomal.xml;

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffffff"/>
<corners android:radius="8dip"/>
</shape>

acitivity_main.xml里的布局;

<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/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"

/>
<LinearLayout
android:id="@+id/yuandian"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentBottom="true"
></LinearLayout>
</RelativeLayout>

在layout里面建一個dot.xml;

<?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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM