Cascade (rounded, card effect)

Attach renderings

1. The need to join dependence

2. The adapter waterfall stream

3. Modify the class rounded picture

4. Create an entity class

5.MainActivity home page Code

6.MainActivity layout file

7. list layout file

8. Finally, attach the project

 

Add dependency needs

implementation 'com.android.support:recyclerview-v7:25.3.1' // set the waterfall list of 
implementation 'com.github.bumptech.glide: glide: 4.9.0' // get a rounded picture and implementation 'com.google .android.material: material: 1.0.0 '// card effect

 In joining the network permissions

<uses-permission android:name="android.permission.INTERNET"/>

 

Adapter waterfall stream

. 1  public  class RecyclerViewAdapter the extends RecyclerView.Adapter <RecyclerViewAdapter.MyViewHolder> {
 2  
. 3      Private the Context context;
 . 4      Private List <Menu> List; // data 
. 5      Private List <Integer> heightList; // random number output means 
. 6  
. 7      public RecyclerViewAdapter (the context context, List <Menu> List) {
 . 8          the this .context = context;
 . 9          the this .list = List;
 10          // record the height of each control randomly generated to avoid slip back to top blank 
11         heightList = new ArrayList<>();
12         for (int i = 0; i < list.size(); i++) {
13             int height = new Random().nextInt(300) + 500;//[100,300)的随机数
14             heightList.add(height);
15         }
16     }
17 
18     @Override
19     public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
20         //找到item的布局
21         View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
22         return new MyViewHolder(view);//将布局设置给holder
23     }
24 
25     @Override
26     public int getItemCount() {
27         return list.size();
28     }
29 
30     @Override
31     public void onBindViewHolder(final MyViewHolder holder, final int position) {
32         //填充数据
33         Options = RequestOptions new new RequestOptions ()
 34 is                  .centerCrop ()
 35                  .placeholder (R.mipmap.ic_launcher_round) // preload image 
36                  .error (R.drawable.ic_launcher_foreground) // load failure image 
37 [                  .priority (Priority.HIGH) // priority 
38 is                  .diskCacheStrategy (DiskCacheStrategy.NONE) // buffer 
39                  .transform ( new new GlideRoundTransform (. 5)); // fillet 
40         . Glide.with (context) .load (List.get (position) .getImage ()) Apply (Options) .into (holder.imageView);
 41 is          holder.menu.setText (List.get (position) .getName () );
 42 is          holder.price.setText (List.get (position) .getPrice ());
 43 is          // because of the need to achieve the effect of the cascade, so we need to dynamically change the height of the control 
44 is          ViewGroup.LayoutParams the params = Holder. imageView.getLayoutParams ();
 45          params.height = heightList.get (position);
 46 is          holder.imageView.setLayoutParams (the params);
 47  
48      }
 49  
50      class MyViewHolder the extends RecyclerView.ViewHolder {
 51 is         ImageView imageView;
52         TextView menu,price;
53 
54         public MyViewHolder(View itemView) {
55             super(itemView);
56             imageView = (ImageView) itemView.findViewById(R.id.imageView);
57             menu = (TextView)itemView.findViewById(R.id.text_menu);
58             price = (TextView)itemView.findViewById(R.id.text_money);
59         }
60     }
61 }

Modify the image rounded class

public class GlideRoundTransform extends BitmapTransformation {
    private static float radius = 0f;

    public GlideRoundTransform() {
        this(4);
    }

    public GlideRoundTransform(int dp) {
        super();
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }


    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        //变换的时候裁切
        Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
        return roundCrop(pool, bitmap);
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

    }


    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) {
            return null;
        }
        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        //左上角、右上角圆角
//        RectF rectRound = new RectF(0f, 100f, source.getWidth(), source.getHeight());
//        canvas.drawRect(rectRound, paint);
        return result;
    }
}

 

Creating an entity class

 

5.MainActivity home page Code

6.MainActivity layout file

7. list layout file

8. Finally, attach the project

Guess you like

Origin www.cnblogs.com/Mr-Deng/p/11533226.html