首先创建小圆点
小圆点的两个属性一共两个属性另一个跟这个一个样,只不过颜色能让我们区分的
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="8dp" />
<solid android:color="#e2061c" />
</shape>
接着小圆点的布局是一下自己看吧
<?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">
<View
android:id="@+id/scrollView"
android:layout_width="8dp"
android:layout_height="8dp"
android:layout_margin="8dp"
android:background="@drawable/dots_normal"
/>
</LinearLayout>
下面是main.xml里面的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/ll"
android:layout_marginTop="450dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="20dp"/>
<Button
android:id="@+id/bt_tiao"
android:text="主页"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
//获取图片需要图片的包
Universal-Image-Loader
下面是代码
public class MyApp extends Application{
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
ImageLoader.getInstance().init(configuration);
}
}
Universal-Image-Loader 导入包
添加权限
<uses-permission android:name="android.permission.INTERNET" />
android:name="MyApp"
下面是src中的代码
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.bwie.bean.SuperClass;
import com.bwie.utils.NetWorkUtils;
import com.bwie.utils.URLDB;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private Button bt;
private LinearLayout layout;
private List<SuperClass.NewslistBean> data;
private NetWorkUtils netWorkUtils = new NetWorkUtils();//实例化工具类
private List<ImageView>imgs = new ArrayList<ImageView>();//图片的集合
private List<View> dots = new ArrayList<View>(); //小圆点集合
private int i = 0;
private int dot = 0;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
viewPager.setCurrentItem(i);//得到传过来的值
dots.get(dot).setBackgroundResource(R.drawable.dots_normal);
dots.get(i%imgs.size()).setBackgroundResource(R.drawable.dots_focus);
dot=i%imgs.size();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//异步网络请求
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... params) {
String json = netWorkUtils.getJsonHttpConnetion(接口);
return json;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
--------通过添加gson包进行解析------
init();//初始化方法
}
}.execute();
}
//初始化方法
private void init() {
viewPager = (ViewPager) findViewById(R.id.viewPager);//获取组件
layout = (LinearLayout) findViewById(R.id.ll);
getImgs();//获取图片
getDots();//获取小圆点
//viewPager适配器
viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return Integer.MAX_VALUE;//最大值
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
int i = position % imgs.size();
ImageView imageView = imgs.get(i);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
});
//设置初始化位置
i = imgs.size()*10;
//得到位置
viewPager.setCurrentItem(i);
//设置第一个圆点为true
dots.get(0).setBackgroundResource(R.drawable.dots_focus);
//timer自动轮播
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
i = viewPager.getCurrentItem() + 1;//
handler.sendEmptyMessage(i);//发送数据
}
},1,2000);
}
//图片的方法
private void getImgs() {
for(int i = 0;i<data.size();i++){
String picUrl = data.get(i).getPicUrl();
ImageView img = new ImageView(this);
img.setScaleType(ImageView.ScaleType.FIT_XY);
ImageLoader.getInstance().displayImage(picUrl,img);
imgs.add(img);
}
}
//小圆点的方法
private void getDots() {
for(int i=0;i<imgs.size();i++){
View view = LayoutInflater.from(this).inflate(R.layout.dot_item,null);
View viewById = view.findViewById(R.id.scrollView);
dots.add(viewById);
layout.addView(view);
}
}
}
多多支持多多点赞
- 顶
- 1