您的位置:首页 > 移动开发 > Android开发

android 瀑布流的实现(用recyclerview的实现的)

2016-03-25 18:47 495 查看
先看下效果图 



代码的整体布局:



首先要做的就是导入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.RecyclerView
android: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">
<ImageView
android: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"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.example.recyclerviewtest.CircleImageView
android: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"/>

<TextView
android: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"/>

<ImageView
android: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"/>
<LinearLayout

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<ImageView
android: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"/>
<TextView
android: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"/>

<TextView
android: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"/>

<TextView
android:id="@+id/recyclerview_label3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="甜美"
android:textSize="12sp"
android:textColor="#000000"/>

</LinearLayout>

</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#330000"/>
<TextView
android:id="@+id/recyclerview_mood"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="今天的拿铁特别好喝,心情超级好!"
android:textColor="#000000"
android:textSize="12sp"/>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android: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" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"
android:orientation="horizontal" >

<ImageView
android:id="@+id/recyclerview_parse"
android:layout_width="15dp"
android:layout_height="15dp"
android:adjustViewBounds="true"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_launcher" />

<TextView
android: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>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:orientation="horizontal" >

<ImageView
android:id="@+id/recyclerview_comment"
android:layout_width="15dp"
android:layout_height="15dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher" />

<TextView
android: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;
@Override
protected 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() {
@Override
public 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;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public 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() {
@Override
public void onClick(View v) {
int pos = holder.getLayoutPosition();//得到当前点击item的位置pos
mListener.ItemClickListener(holder.itemView,pos);//把事件交给我们实现的接口那里处理
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int pos = holder.getLayoutPosition();//得到当前点击item的位置pos
mListener.ItemClickListener(holder.itemView,pos);//把事件交给我们实现的接口那里处理
return true;
}
});
}
}
@Override
public 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;
}
}

@Override
public ScaleType getScaleType() {
return SCALE_TYPE;
}

@Override
public void setScaleType(ScaleType scaleType) {
if (scaleType != SCALE_TYPE) {
throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));
}
}

@Override
protected void onDraw(Canvas canvas) {
if (getDrawable() == null) {
return;
}

canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint);
}

@Override
protected 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();
}

@Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
mBitmap = bm;
setup();
}

@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
mBitmap = getBitmapFromDrawable(drawable);
setup();
}

@Override
public 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不熟悉,可以去看看他的博客,

这里没有图小图片进行处理,有兴趣的可以放几张小的图片看看效果

源码下载,记得点赞奥!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  瀑布流 recyclerview