初始化ImageLoader
package com.bwei.application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import android.app.Application;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 初始化ImageLoader
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
this).memoryCacheSize(2 * 1024 * 1024).build();
ImageLoader.getInstance().init(configuration);
}
}
//图片边距
ScaleType scaleType=ScaleType.FIT_XY;
iv.setScaleType(scaleType);
//加载图片
ImageLoader.getInstance().displayImage(booklist.get(position).coverImg, iv);
//加载图片
BitmapUtils utils = new BitmapUtils(context);
utils.display(iv1, glist.get(position).imgurl);
自动轮播+Fragment 代码:
package com.bwei.fragment;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import com.bwei.adapter.Myf1gvAdapter;
import com.bwei.adapter.Myf1pageAdapter;
import com.bwei.fragment.item.F1gvActivity;
import com.bwei.utils.EntWorkUtils;
import com.bwei.uu.R;
import com.bwei.vo.Category;
import com.bwei.vo.ComicBook;
import com.bwei.vo.ComicBook.Result.BookList;
import com.google.gson.Gson;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
public class FragMent1 extends Fragment {
private View v;
private ViewPager vp;
private LinearLayout llt;
private List<String> blist;
private String json;
private LinearLayout lin;
private GridView gv;
int[] img = new int[] { R.drawable.haizei1, R.drawable.v6, R.drawable.v7,
R.drawable.v8, R.drawable.v9 };
List<ImageView> ivlist = new ArrayList<ImageView>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment1, null);
// 控件
find();
// 解析
getData();
// 适配器
vp.setAdapter(new Myf1pageAdapter(getActivity(), img));
initDot();
vp.setCurrentItem(100000);
setTimer();
// 监听事件
pageListener();
// 监听事件
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// 进入跳转
Intent intent = new Intent(getActivity(), F1gvActivity.class);
intent.putExtra("type", blist.get(position));
startActivity(intent);
}
});
return v;
}
// 监听事件
private void pageListener() {
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
for (int i = 0; i < ivlist.size(); i++) {
if (position % ivlist.size() == i) {
// 同步
ivlist.get(position % ivlist.size()).setImageResource(
R.drawable.dot_focuse);
} else {
// 不同步
ivlist.get(i).setImageResource(R.drawable.dot_normal);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
// 计时
private void setTimer() {
Timer tim = new Timer();
tim.schedule(new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// 当前索引值
int item = vp.getCurrentItem();
item++;
vp.setCurrentItem(item);
}
});
}
}, 0, 3000);
}
private void initDot() {
if (ivlist != null) {
ivlist.clear();
}
for (int i = 0; i < img.length; i++) {
ImageView iv = new ImageView(getActivity());
if (i == 0) {
// 默认图片
iv.setImageResource(R.drawable.dot_focuse);
} else {
iv.setImageResource(R.drawable.dot_normal);
}
ivlist.add(iv);
// 图片大小
LayoutParams params = new LayoutParams(12, 12);
// 图片间距
params.setMargins(10, 0, 10, 0);
lin.addView(iv, params);
}
}
// 异步交互
private void getData() {
// TODO Auto-generated method stub
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
//路径
json = EntWorkUtils
.getStr("http://japi.juhe.cn/comic/category?key=eddbc1ce67ba5ede8e31a69adc40c315");
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// gson解析
Gson gson = new Gson();
Category shonen2 = gson.fromJson(json, Category.class);
blist = shonen2.result;
// gv适配器
gv.setAdapter(new Myf1gvAdapter(getActivity(), blist));
}
}.execute();
}
// 控件
private void find() {
vp = (ViewPager) v.findViewById(R.id.vpimg);
llt = (LinearLayout) v.findViewById(R.id.llt);
lin = (LinearLayout) v.findViewById(R.id.lin);
gv = (GridView) v.findViewById(R.id.gv);
llt.getBackground().setAlpha(150);
}
}
ViewPager 适配器
package com.bwei.adapter;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class Myf1pageAdapter extends PagerAdapter {
Context context;
int[] img;
public Myf1pageAdapter(Context context, int[] img) {
this.context=context;
this.img=img;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@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((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView iv=new ImageView(context);
ScaleType scaleType=ScaleType.FIT_XY;
iv.setScaleType(scaleType);
iv.setImageResource(img[position%img.length]);
container.addView(iv);
return iv;
}
}
GridView 适配器
package com.bwei.adapter;
import java.util.List;
import com.bwei.uu.R;
import com.bwei.vo.ComicBook.Result.BookList;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ListAdapter;
import android.widget.TextView;
public class Myf1gvAdapter extends BaseAdapter implements ListAdapter {
Context context;
List<String> blist;
int[] ids = new int[]{R.drawable.sn12,R.drawable.qn1,R.drawable.sn11,R.drawable.lm12};
public Myf1gvAdapter(Context context, List<String> blist) {
this.blist=blist;
this.context=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return blist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
convertView=View.inflate(context, R.layout.gv_tem, null);
}
ImageView iv=(ImageView) convertView.findViewById(R.id.imageView1);
TextView tname=(TextView) convertView.findViewById(R.id.tname);
tname.setText(blist.get(position).toString());
ScaleType scaleType=ScaleType.FIT_XY;
iv.setScaleType(scaleType);
iv.setImageResource(ids[position]);
return convertView;
}
}