XUtiles中BitmapUtils的使用方法

来源:互联网 发布:网络电视能连跳舞毯吗 编辑:程序博客网 时间:2024/06/06 00:06

 





首先导入依赖

compile 'org.xutils:xutils:3.5.0'


然后在libs中加一个jar包



                    先上布局文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="sqs.jcc.com.cuifan20170620.MainActivity">  <GridView      android:id="@+id/photo_wall"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:columnWidth="100dp"      android:gravity="center"      android:horizontalSpacing="1dp"      android:numColumns="auto_fit"      android:stretchMode="columnWidth"      android:verticalSpacing="1dp" >  </GridView></RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/photo"        android:layout_width="150dp"        android:layout_height="150dp"        android:layout_centerInParent="true"        android:scaleType="fitXY"        /></LinearLayout>
接着初始化    别application注册

public class App extends Application {    @Override    public void onCreate() {        super.onCreate();        x.Ext.init(this);    }}
适配器

public class GridViewAdapter extends BaseAdapter {    public LayoutInflater inflater;    public String[] imageUrls;    public BitmapUtils bitmapUtils;    public GridViewAdapter(Context context, String[] imageUrls, BitmapUtils bitmapUtils) {        inflater = LayoutInflater.from(context);        this.imageUrls = imageUrls;        this.bitmapUtils = bitmapUtils;    }    @Override    public int getCount() {        return imageUrls.length;    }    @Override    public String getItem(int position) {        return imageUrls[position];    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        String currStr = getItem(position);        View view;        ViewHolder holder;        if(convertView == null)        {            view = inflater.inflate(R.layout.gridview, null);            holder = new ViewHolder();            holder.imageView = (ImageView) view.findViewById(R.id.photo);            view.setTag(holder);        }else        {            view = convertView;            holder = (ViewHolder) view.getTag();        }        //为防止图片出现乱序,对每个ImageView进行绑定设置        holder.imageView.setTag(currStr);        //设置默认的显示图片        holder.imageView.setImageResource(R.drawable.empty_photo);        showBitmapToImageView(currStr, holder.imageView);        return view;    }    /**     * 设置Bitmap图片到ImageView上面     * @param url     * @param imageView     */    public void showBitmapToImageView(String url,ImageView imageView)    {        bitmapUtils.display(imageView,url);    }    static class ViewHolder    {        ImageView imageView;    }}
放图片的Url

public class Utils {   public final static String[] imageUrls = new String[] {          "http://img.my.csdn.net/uploads/201407/26/1406383299_1976.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383291_6518.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383291_8239.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383290_9329.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383290_1042.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383275_3977.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383265_8550.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383264_3954.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383264_4787.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383264_8243.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383248_3693.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383243_5120.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383242_3127.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383242_9576.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383242_1721.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383219_5806.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383214_7794.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383213_4418.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383213_3557.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383210_8779.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383172_4577.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383166_3407.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383166_2224.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383166_7301.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383165_7197.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383150_8410.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383131_3736.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383130_5094.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383130_7393.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383129_8813.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383100_3554.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383093_7894.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383092_2432.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383092_3071.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383091_3119.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383059_6589.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383059_8814.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383059_2237.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383058_4330.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406383038_3602.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382942_3079.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382942_8125.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382942_4881.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382941_4559.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382941_3845.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382924_8955.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382923_2141.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382923_8437.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382922_6166.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382922_4843.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382905_5804.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382904_3362.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382904_2312.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382904_4960.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382900_2418.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382881_4490.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382881_5935.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382880_3865.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382880_4662.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382879_2553.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382862_5375.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382862_1748.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382861_7618.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382861_8606.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382861_8949.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382841_9821.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382840_6603.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382840_2405.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382840_6354.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382839_5779.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382810_7578.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382810_2436.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382809_3883.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382809_6269.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382808_4179.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382790_8326.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382789_7174.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382789_5170.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382789_4118.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382788_9532.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382767_3184.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382767_4772.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382766_4924.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382766_5762.jpg",          "http://img.my.csdn.net/uploads/201407/26/1406382765_7341.jpg"      };        /**    * ��ȡ���̻���Ĵ洢·��    * @param context    * @param uniqueName    * @return    */   public static String getDiskCacheDir(Context context,String uniqueName)   {      String filePath = "";      if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable())      {               filePath = context.getExternalCacheDir().getPath();      }else      {         filePath = context.getCacheDir().getPath();      }      return filePath+File.separator+uniqueName;   }}
MainActivit中

public class MainActivity extends AppCompatActivity {    private GridView gv;    private GridView photo_wall;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        photo_wall = (GridView) findViewById(R.id.photo_wall);        String filePath = Utils.getDiskCacheDir(this, "bitmap");        File directory  = new File(filePath);        if(!directory.exists())        {            directory.mkdirs();        }        BitmapUtils bitmapUtils = new BitmapUtils(this, filePath);        GridViewAdapter adapter = new GridViewAdapter(this, Utils.imageUrls, bitmapUtils);        photo_wall.setAdapter(adapter);    }}
                                                       最后记得加上权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
                                                                           GitHub 下载地址       https://github.com/DemoCuiFan/cuifan20170620

原创粉丝点击