• 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

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

  • 相关阅读:
    XML及XML的解析
    单例设计模式(Singleton)的优化
    Java反射初识
    TCP协议的简单应用一
    Java中实现线程同步的三种方法
    Java集合框架Map接口
    JDK1.8新特性之Stream类初识
    Java JDK1.8新特性之四大函数式接口
    tomcat 启动报 找不到 StrutsPrepareAndExecuteFilter。。
    easyjweb ejs 2014.2.25
  • 原文地址:https://www.cnblogs.com/kunyashaw/p/5110708.html
Copyright © 2020-2023  润新知