android 瀑布流的实现(用recyclerview的实现的)
生活随笔
收集整理的這篇文章主要介紹了
android 瀑布流的实现(用recyclerview的实现的)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先看下效果圖?
代碼的整體布局:
首先要做的就是導入v7包,這個v7的位置就在自己的sdk目錄下,具體位置
我的博客說過了,這里就不再說了,
下面就是布局代碼
main_activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><android.support.v7.widget.RecyclerViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/recyclerView"></android.support.v7.widget.RecyclerView>
</RelativeLayout>item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:orientation="vertical"android:layout_height="wrap_content"android:background="@drawable/recyclerview_item"android:layout_margin="5dp"> <ImageViewandroid:id="@+id/recyclerviewicon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:scaleType="fitXY"android:layout_gravity="center"android:adjustViewBounds="true"android:maxWidth="300dp"android:maxHeight="600dp"android:src="@drawable/ic_launcher"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:orientation="vertical"> <RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"> <com.example.recyclerviewtest.CircleImageViewandroid:id="@+id/recyclerview_pic_h"android:layout_width="40dp"android:layout_height="40dp"android:layout_marginLeft="10dp"android:src="@drawable/ic_launcher"app:border_width="0.5dp"app:border_color="#000000"/><TextViewandroid:id="@+id/recyclerview_peo_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Baby"android:textSize="12sp"android:textColor="#000000"android:layout_marginLeft="60dp"/><ImageViewandroid:id="@+id/recyclerview_peo_sex"android:layout_width="wrap_content"android:layout_height="15dp"android:adjustViewBounds="true"android:src="@drawable/ic_launcher"android:layout_marginLeft="90dp"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="60dp"android:layout_marginTop="20dp"android:orientation="horizontal"><ImageViewandroid:id="@+id/recyclerview_label_pic"android:layout_width="wrap_content"android:layout_height="15dp"android:adjustViewBounds="true"android:src="@drawable/ic_launcher"android:paddingRight="8dp"/><TextViewandroid:id="@+id/recyclerview_label1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="白領"android:textSize="12sp"android:textColor="#000000"android:paddingRight="5dp"/><TextViewandroid:id="@+id/recyclerview_label2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="發呆ing"android:textSize="12sp"android:textColor="#000000"android:paddingRight="5dp"/><TextViewandroid:id="@+id/recyclerview_label3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="甜美"android:textSize="12sp"android:textColor="#000000"/></LinearLayout></RelativeLayout><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="#330000"/><TextViewandroid:id="@+id/recyclerview_mood"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="今天的拿鐵特別好喝,心情超級好!"android:textColor="#000000"android:textSize="12sp"/><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/recyclerview_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="15dp"android:background="@null"android:text="12-16 16:42"android:textColor="#000000"android:textSize="12sp" /><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_marginRight="50dp"android:orientation="horizontal" ><ImageViewandroid:id="@+id/recyclerview_parse"android:layout_width="15dp"android:layout_height="15dp"android:adjustViewBounds="true"android:layout_marginLeft="10dp"android:src="@drawable/ic_launcher" /><TextViewandroid:id="@+id/recyclerview_parse_num"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="35"android:layout_marginRight="10dp"android:textColor="#000000"android:textSize="12sp" /></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_marginRight="20dp"android:orientation="horizontal" ><ImageViewandroid:id="@+id/recyclerview_comment"android:layout_width="15dp"android:layout_height="15dp"android:adjustViewBounds="true"android:src="@drawable/ic_launcher" /><TextViewandroid:id="@+id/recyclerview_com_num"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="評論"android:textColor="#000000"android:textSize="12sp" /></LinearLayout></RelativeLayout> </LinearLayout>
</LinearLayout>java 代碼
mainactivity
package com.example.recyclerviewtest;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;import java.util.ArrayList;
import java.util.List;import com.example.recyclerviewtest.MyRecyclerAdapter.OnItemClickListener;import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
@SuppressWarnings("deprecation")
public class MainActivity extends ActionBarActivity {private RecyclerView mRecyclerView;private List<RecyclerBean> lists =new ArrayList<RecyclerBean>();private RecyclerBean content;private MyRecyclerAdapter adapter; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initData();mRecyclerView = (RecyclerView) this.findViewById(R.id.recyclerView);mRecyclerView.setItemAnimator(new DefaultItemAnimator());mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));adapter = new MyRecyclerAdapter(this,lists);mRecyclerView.setAdapter(adapter); adapter.setOnClickListener(new OnItemClickListener() { @Overridepublic void ItemClickListener(View view, int postion) { Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); //Log.i("msg",bm.getHeight()+"-----"+bm.getWidth());Toast.makeText(MainActivity.this,"點擊了:"+postion+bm.getHeight()+"------"+bm.getWidth(),Toast.LENGTH_SHORT).show(); }});}private List<RecyclerBean> initData() {if (lists == null ) {lists = new ArrayList<RecyclerBean>();}for (int i = 0; i <20; i++) {if (i==0) { content = new RecyclerBean(R.drawable.a,R.drawable.a,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,"小木","勇士來杯冰瓶酒吧","5-6 19-41","32","12","醫生","教師","學生"); }else if (i==1) {content = new RecyclerBean(R.drawable.b,R.drawable.b,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,"小白菜","今天心情很不好","5-6 19-41","32","12","醫生","教師","學生");}else if (i==2) {content = new RecyclerBean(R.drawable.c,R.drawable.c,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,"小王","我的劍就是你的劍","5-6 19-41","32","12","醫生","教師","學生");}else if (i==3) {content = new RecyclerBean(R.drawable.circle_logo1,R.drawable.circle_logo1,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,"小貝","搖搖晃晃,召喚師知道我是誰嗎?","5-6 19-41","32","12","醫生","教師","學生");}else if (i==4) {content = new RecyclerBean(R.drawable.circle_view,R.drawable.circle_view,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,"聶溁","今天心情很不好","5-6 19-41","32","12","醫生","教師","學生");}else if (i==5) {content = new RecyclerBean(R.drawable.de,R.drawable.de,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,"浪貨","今天心情很不好","5-6 19-41","32","12","醫生","教師","學生");}else if (i==6) {content = new RecyclerBean(R.drawable.em,R.drawable.em,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,"小鳳","今天心情很不好","5-6 19-41","32","12","醫生","教師","學生");}else if (i==7) {content = new RecyclerBean(R.drawable.icon1,R.drawable.icon1,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,"老王","今天心情很不好","5-6 19-41","32","12","醫生","教師","學生");}else if (i==8) { content = new RecyclerBean(R.drawable.im1,R.drawable.im1,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,"阿道夫","今天心情很不好","5-6 19-41","32","12","醫生","教師","學生");} else if (i==9) {content = new RecyclerBean(R.drawable.c,R.drawable.im1,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,"阿道夫","今天心情很不好","5-6 19-41","32","12","醫生","教師","學生");}lists.add(content); }return lists;}
}
myrecyclerviewadapter
package com.example.recyclerviewtest;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> {private List<RecyclerBean> lists ; private Context context;private OnItemClickListener mListener;public MyRecyclerAdapter(Context context,List<RecyclerBean> lists) {this.context = context;this.lists = lists;}public interface OnItemClickListener{void ItemClickListener(View view,int postion);}public void setOnClickListener(OnItemClickListener listener){this.mListener = listener;}@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(context).inflate(R.layout.item,parent,false);MyViewHolder viewHolder = new MyViewHolder(view);return viewHolder;}@Overridepublic void onBindViewHolder(final MyViewHolder holder, int position) {ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();//得到item的LayoutParams布局參數holder.itemView.setLayoutParams(params);//把params設置給item布局 holder.recyclerview_peo_name.setText(lists.get(position).getRecyclerview_peo_name());holder.recyclerview_mood.setText(lists.get(position).getRecyclerview_mood());holder.recyclerview_time.setText(lists.get(position).getRecyclerview_time());holder.recyclerview_parse_num.setText(lists.get(position).getRecyclerview_parse_num());holder.recyclerview_com_num.setText(lists.get(position).getRecyclerview_com_num());holder.recyclerview_label1.setText(lists.get(position).getRecyclerview_label1());holder.recyclerview_label2.setText(lists.get(position).getRecyclerview_label2());holder.recyclerview_label3.setText(lists.get(position).getRecyclerview_label3()); holder.recyclerviewicon.setImageResource(lists.get(position).getRecyclerviewicon());holder.recyclerview_pic_h.setImageResource(lists.get(position).getRecyclerview_pic_h());holder.recyclerview_peo_sex.setImageResource(lists.get(position).getRecyclerview_peo_sex());holder.recyclerview_parse.setImageResource(lists.get(position).getRecyclerview_parse());holder.recyclerview_comment.setImageResource(lists.get(position).getRecyclerview_comment());holder.recyclerview_label_pic.setImageResource(lists.get(position).getRecyclerview_label_pic());if(mListener!=null){//如果設置了監聽那么它就不為空,然后回調相應的方法holder.itemView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {int pos = holder.getLayoutPosition();//得到當前點擊item的位置posmListener.ItemClickListener(holder.itemView,pos);//把事件交給我們實現的接口那里處理}});holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {int pos = holder.getLayoutPosition();//得到當前點擊item的位置posmListener.ItemClickListener(holder.itemView,pos);//把事件交給我們實現的接口那里處理return true;}});}}@Overridepublic int getItemCount() {return lists.size();}
}
class MyViewHolder extends RecyclerView.ViewHolder{TextView recyclerview_peo_name,recyclerview_mood,recyclerview_time, recyclerview_parse_num,recyclerview_com_num,recyclerview_label1,recyclerview_label2,recyclerview_label3;ImageView recyclerviewicon,recyclerview_pic_h,recyclerview_peo_sex,recyclerview_share_table,recyclerview_parse,recyclerview_comment,recyclerview_label_pic;public MyViewHolder(View itemView) {super(itemView);recyclerview_peo_name = (TextView) itemView.findViewById(R.id.recyclerview_peo_name);recyclerview_mood = (TextView) itemView.findViewById(R.id.recyclerview_mood);recyclerview_time = (TextView) itemView.findViewById(R.id.recyclerview_time);recyclerview_parse_num =(TextView) itemView.findViewById(R.id.recyclerview_parse_num);recyclerview_com_num = (TextView) itemView.findViewById(R.id.recyclerview_com_num);recyclerview_label1 = (TextView) itemView.findViewById(R.id.recyclerview_label1);recyclerview_label2 = (TextView) itemView.findViewById(R.id.recyclerview_label2);recyclerview_label3 = (TextView) itemView.findViewById(R.id.recyclerview_label3);recyclerviewicon = (ImageView) itemView.findViewById(R.id.recyclerviewicon);recyclerview_pic_h = (ImageView) itemView.findViewById(R.id.recyclerview_pic_h);recyclerview_peo_sex = (ImageView) itemView.findViewById(R.id.recyclerview_peo_sex);recyclerview_parse = (ImageView) itemView.findViewById(R.id.recyclerview_parse);recyclerview_comment = (ImageView) itemView.findViewById(R.id.recyclerview_comment);recyclerview_label_pic = (ImageView) itemView.findViewById(R.id.recyclerview_label_pic);}
}recyclerviewbean
package com.example.recyclerviewtest;import java.io.Serializable;import android.R.integer;public class RecyclerBean implements Serializable {public int recyclerviewicon,recyclerview_pic_h,recyclerview_peo_sex,recyclerview_parse,recyclerview_comment,recyclerview_label_pic;public String recyclerview_peo_name,recyclerview_mood,recyclerview_time, recyclerview_parse_num,recyclerview_com_num,recyclerview_label1,recyclerview_label2,recyclerview_label3;public RecyclerBean(int recyclerviewicon, int recyclerview_pic_h,int recyclerview_peo_sex,int recyclerview_parse,int recyclerview_comment,int recyclerview_label_pic, String recyclerview_peo_name,String recyclerview_mood,String recyclerview_time,String recyclerview_parese_num,String recyclerview_com_num,String recyclerview_label1,String recyclerview_label2,String recyclerview_label3){super();this.recyclerviewicon = recyclerviewicon;this.recyclerview_pic_h =recyclerview_pic_h;this.recyclerview_peo_sex=recyclerview_peo_sex;this.recyclerview_parse=recyclerview_parse;this.recyclerview_comment = recyclerview_comment;this.recyclerview_peo_name=recyclerview_peo_name;this.recyclerview_mood = recyclerview_mood;this.recyclerview_time = recyclerview_time;this.recyclerview_parse_num =recyclerview_parese_num;this.recyclerview_com_num = recyclerview_com_num;this.recyclerview_label_pic =recyclerview_label_pic;this.recyclerview_label1 = recyclerview_label1;this.recyclerview_label2 = recyclerview_label2;this.recyclerview_label3 = recyclerview_label3; }public int getRecyclerviewicon() {return recyclerviewicon;}public void setRecyclerviewicon(int recyclerviewicon) {this.recyclerviewicon = recyclerviewicon;}public int getRecyclerview_label_pic() {return recyclerview_label_pic;}public void setRecyclerview_label_pic(int recyclerview_label_pic) {this.recyclerview_label_pic = recyclerview_label_pic;}public String getRecyclerview_label1() {return recyclerview_label1;}public void setRecyclerview_label1(String recyclerview_label1) {this.recyclerview_label1 = recyclerview_label1;}public String getRecyclerview_label2() {return recyclerview_label2;}public void setRecyclerview_label2(String recyclerview_label2) {this.recyclerview_label2 = recyclerview_label2;}public String getRecyclerview_label3() {return recyclerview_label3;}public void setRecyclerview_label3(String recyclerview_label3) {this.recyclerview_label3 = recyclerview_label3;}public int getRecyclerview_pic_h() {return recyclerview_pic_h;}public void setRecyclerview_pic_h(int recyclerview_pic_h) {this.recyclerview_pic_h = recyclerview_pic_h;}public int getRecyclerview_peo_sex() {return recyclerview_peo_sex;}public void setRecyclerview_peo_sex(int recyclerview_peo_sex) {this.recyclerview_peo_sex = recyclerview_peo_sex;}public int getRecyclerview_parse() {return recyclerview_parse;}public void setRecyclerview_parse(int recyclerview_parse) {this.recyclerview_parse = recyclerview_parse;}public int getRecyclerview_comment() {return recyclerview_comment;}public void setRecyclerview_comment(int recyclerview_comment) {this.recyclerview_comment = recyclerview_comment;}public String getRecyclerview_peo_name() {return recyclerview_peo_name;}public void setRecyclerview_peo_name(String recyclerview_peo_name) {this.recyclerview_peo_name = recyclerview_peo_name;}public String getRecyclerview_mood() {return recyclerview_mood;}public void setRecyclerview_mood(String recyclerview_mood) {this.recyclerview_mood = recyclerview_mood;}public String getRecyclerview_time() {return recyclerview_time;}public void setRecyclerview_time(String recyclerview_time) {this.recyclerview_time = recyclerview_time;}public String getRecyclerview_parse_num() {return recyclerview_parse_num;}public void setRecyclerview_parse_num(String recyclerview_parse_num) {this.recyclerview_parse_num = recyclerview_parse_num;}public String getRecyclerview_com_num() {return recyclerview_com_num;}public void setRecyclerview_com_num(String recyclerview_com_num) {this.recyclerview_com_num = recyclerview_com_num;} }
圓形頭像的
package com.example.recyclerviewtest;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;public class CircleImageView extends ImageView {private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;private static final int COLORDRAWABLE_DIMENSION = 1;private static final int DEFAULT_BORDER_WIDTH = 0;private static final int DEFAULT_BORDER_COLOR = Color.BLACK;private final RectF mDrawableRect = new RectF();private final RectF mBorderRect = new RectF();private final Matrix mShaderMatrix = new Matrix();private final Paint mBitmapPaint = new Paint();private final Paint mBorderPaint = new Paint();private int mBorderColor = DEFAULT_BORDER_COLOR;private int mBorderWidth = DEFAULT_BORDER_WIDTH;private Bitmap mBitmap;private BitmapShader mBitmapShader;private int mBitmapWidth;private int mBitmapHeight;private float mDrawableRadius;private float mBorderRadius;private boolean mReady;private boolean mSetupPending;public CircleImageView(Context context) {super(context);}public CircleImageView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public CircleImageView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);super.setScaleType(SCALE_TYPE);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0);mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH);mBorderColor = a.getColor(R.styleable.CircleImageView_border_color, DEFAULT_BORDER_COLOR);a.recycle();mReady = true;if (mSetupPending) {setup();mSetupPending = false;}}@Overridepublic ScaleType getScaleType() {return SCALE_TYPE;}@Overridepublic void setScaleType(ScaleType scaleType) {if (scaleType != SCALE_TYPE) {throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));}}@Overrideprotected void onDraw(Canvas canvas) {if (getDrawable() == null) {return;}canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint);canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint);}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);setup();}public int getBorderColor() {return mBorderColor;}public void setBorderColor(int borderColor) {if (borderColor == mBorderColor) {return;}mBorderColor = borderColor;mBorderPaint.setColor(mBorderColor);invalidate();}public int getBorderWidth() {return mBorderWidth;}public void setBorderWidth(int borderWidth) {if (borderWidth == mBorderWidth) {return;}mBorderWidth = borderWidth;setup();}@Overridepublic void setImageBitmap(Bitmap bm) {super.setImageBitmap(bm);mBitmap = bm;setup();}@Overridepublic void setImageDrawable(Drawable drawable) {super.setImageDrawable(drawable);mBitmap = getBitmapFromDrawable(drawable);setup();}@Overridepublic void setImageResource(int resId) {super.setImageResource(resId);mBitmap = getBitmapFromDrawable(getDrawable());setup();}private Bitmap getBitmapFromDrawable(Drawable drawable) {if (drawable == null) {return null;}if (drawable instanceof BitmapDrawable) {return ((BitmapDrawable) drawable).getBitmap();}try {Bitmap bitmap;if (drawable instanceof ColorDrawable) {bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);} else {bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);}Canvas canvas = new Canvas(bitmap);drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());drawable.draw(canvas);return bitmap;} catch (OutOfMemoryError e) {return null;}}private void setup() {if (!mReady) {mSetupPending = true;return;}if (mBitmap == null) {return;}mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);mBitmapPaint.setAntiAlias(true);mBitmapPaint.setShader(mBitmapShader);mBorderPaint.setStyle(Paint.Style.STROKE);mBorderPaint.setAntiAlias(true);mBorderPaint.setColor(mBorderColor);mBorderPaint.setStrokeWidth(mBorderWidth);mBitmapHeight = mBitmap.getHeight();mBitmapWidth = mBitmap.getWidth();mBorderRect.set(0, 0, getWidth(), getHeight());mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2, (mBorderRect.width() - mBorderWidth) / 2);mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width() - mBorderWidth, mBorderRect.height() - mBorderWidth);mDrawableRadius = Math.min(mDrawableRect.height() / 2, mDrawableRect.width() / 2);updateShaderMatrix();invalidate();}private void updateShaderMatrix() {float scale;float dx = 0;float dy = 0;mShaderMatrix.set(null);if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {scale = mDrawableRect.height() / (float) mBitmapHeight;dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;} else {scale = mDrawableRect.width() / (float) mBitmapWidth;dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;}mShaderMatrix.setScale(scale, scale);mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth);mBitmapShader.setLocalMatrix(mShaderMatrix);}}額忘記了,圓形頭像又attrs的布局 這個貼出來吧,在values
下面 當然大家根據自己的需求,可以把頭像刪除掉就行
<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="CircleImageView"><attr name="border_width" format="dimension" /><attr name="border_color" format="color" /></declare-styleable>
</resources>
本文參考:hongyang 的博客,如果對recyclerview不熟悉,可以去看看他的博客,
這里沒有圖小圖片進行處理,有興趣的可以放幾張小的圖片看看效果
源碼下載,記得點贊奧!!!!
總結
以上是生活随笔為你收集整理的android 瀑布流的实现(用recyclerview的实现的)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 的中文意思
- 下一篇: 怎样往csdn上传自己的demo