kunyashaw博客主页 关注kunyashaw新博客 关于kunyashaw 转到底部

69、Android 布局中轻松实现图片的全屏、居中、平铺


 1   public void paint() {
 2         if (item.laying_mode != 1)//平铺或者充满
 3         {
 4             new AsyncTask<Void, Void, Void>() {
 5                 @Override
 6                 protected Void doInBackground(Void... params) {
 7                     Looper.prepare();
 8                     try {
 9                         theBitmap = Glide.
10                                 with(ctxt).
11                                 load(item.src).
12                                 asBitmap().
13                                 into(fenbianlv[0], fenbianlv[1]).
14                                 get();
15                     } catch (final ExecutionException e) {
16                         Log.e(TAG, e.getMessage());
17                     } catch (final InterruptedException e) {
18                         Log.e(TAG, e.getMessage());
19                     }
20                     return null;
21                 }
22 
23                 @Override
24                 protected void onPostExecute(Void dummy) {
25                     if (null != theBitmap) {
26                         // The full bitmap should be available here
27 
28                         BitmapDrawable bd = new BitmapDrawable(Resources.getSystem(),theBitmap);
29                         if (item.laying_mode == 0)//充满(默认值,缩放到图片全部充满屏幕
30                         {
31                         } else if (item.laying_mode == 2)//平铺(从左上开始,一个一个的平铺)
32                         {
33                             bd.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
34                         }
35                         rl.setBackground(bd);
36                     }
37                 }
38             }.execute();
39         }
40         else//1:居中(图片按照原始大小,放到屏幕中间)
41         {
42             new AsyncTask<Void, Void, Void>() {
43                 @Override
44                 protected Void doInBackground(Void... params) {
45                     Looper.prepare();
46                     try {
47                         String imageUrl = item.src;
48                         theBitmap= BitmapFactory.decodeStream((InputStream) new URL(imageUrl).getContent());
49                     } catch (final Exception e) {
50                         Log.e(TAG, e.getMessage());
51                     }
52                     return null;
53                 }
54 
55                 @Override
56                 protected void onPostExecute(Void dummy) {
57                     if (null != theBitmap) {
58                         iv.setImageBitmap(theBitmap);
59                     }
60                 }
61             }.execute();
62         }
63     }
View Code

 

 一下三种分别是充满、平铺、居中(按图片原始大小)

 

posted @ 2016-01-07 17:48  kunyashaw  阅读(2101)  评论(0编辑  收藏  举报
回到顶部