admin 管理员组文章数量: 887016
当 GridView 为1列时没有任何问题当设置多列就出现图片错位重复问题public class MainActivity extends AppCompatActivity {
private GridView G1;
private LruCache Lru;
String[] img ={
"http://pic1.win4000/pic/6/02/f5ae6d2ae4_250_350.jpg",
"http://pic1.win4000/pic/1/29/381f2a6941_250_350.jpg",
"http://pic1.win4000/pic/d/dc/95bb6142d5_250_350.jpg",
"http://pic1.win4000/pic/7/54/b3b4112b34_250_350.jpg",
"http://pic1.win4000/pic/d/87/642d5de424_250_350.jpg",
"http://pic1.win4000/pic/c/8c/4d004a28f5_250_350.jpg",
"http://pic1.win4000/pic/4/f8/9f2e1e129c_250_350.jpg",
"http://pic1.win4000/pic/8/40/b90bd3f95f_250_350.jpg",
"http://pic1.win4000/pic/d/99/e7b336f4e7_250_350.jpg",
"http://pic1.win4000/pic/1/44/8b046cb009_250_350.jpg",
"http://pic1.win4000/pic/2/19/38ac8d7ad0_250_350.jpg",
"http://pic1.win4000/pic/2/87/73d1cbf175_250_350.jpg",
"http://pic1.win4000/pic/7/c5/cd8f5c8d1b_250_350.jpg",
"http://pic1.win4000/pic/0/6c/38c8f4e423_250_350.jpg",
"http://pic1.win4000/pic/5/c1/7bb3ec563d_250_350.jpg",
"http://pic1.win4000/pic/2/f5/abc84bceac_250_350.jpg"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
G1 = findViewById(R.id.Gridv);
int max = (int) Runtime.getRuntime().maxMemory() / 1024;
int siz = max / 5;
Lru = new LruCache(siz){
@Override
protected int sizeOf(String key, Bitmap value) {
return super.sizeOf(key, value) / 1024;
}
};
G1.setAdapter(new ShiPei());
}
//适配器
class ShiPei extends BaseAdapter{
Img im;
@Override
public int getCount() {
return img.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.imgtu,null);
im =new Img();
im.Iv = convertView.findViewById(R.id.imageView);
convertView.setTag(im);
}else {
im = (Img) convertView.getTag();
}
new YiBu(img[position]).execute(img[position]);
return convertView;
}
class Img{
ImageView Iv;
}
class YiBu extends AsyncTask{
String url;
public YiBu(String x){
url = x;
}
@Override
protected Bitmap doInBackground(String... strings) {
Bitmap bitmap;
String u1 = strings[0];
bitmap = Lru.get(u1);
if(bitmap == null){
bitmap = getBitmap(u1);
Lru.put(u1,bitmap);
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
im.Iv.setImageBitmap(bitmap);
}
public Bitmap getBitmap(String s){
Bitmap b = null;
OkHttpClient client = new OkHttpClient();
Request R = new Request.Builder().url(s).build();
try {
Response response = client.newCall(R).execute();
byte[] byt = response.body().bytes();
b = BitmapFactory.decodeByteArray(byt,0,byt.length);
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
}
}
来个大佬看下 网上搜半天什么设置tag验证得方式 也尝试过 一样没用
本文标签: 图片 组件 加载 Android GridView
版权声明:本文标题:android gridview 图片错位,android GridView组件多列加载图片时错位问题 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1726378245h948392.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论