圆形imageview以及Blur
来源:互联网 发布:云端网络成功案例 编辑:程序博客网 时间:2024/05/23 01:26
首先效果图
圆形图片代码部分:
/** * 描述:圆形头像 * 作者:Marc on 2016/8/4 13:20 * 邮箱:aliali_ha@yeah.net */public class RoundImageView 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 = 2; private static final int DEFAULT_BORDER_WIDTH = 0; private static final int DEFAULT_BORDER_COLOR = Color.BLACK; private static final boolean DEFAULT_BORDER_OVERLAY = false; 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 ColorFilter mColorFilter; private boolean mReady; private boolean mSetupPending; private boolean mBorderOverlay; public RoundImageView(Context context) { super(context); init(); } public RoundImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RoundImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.roundedimageview, defStyle, 0); mBorderWidth = a.getDimensionPixelSize(R.styleable.roundedimageview_border_width, DEFAULT_BORDER_WIDTH); mBorderColor = a.getColor(R.styleable.roundedimageview_border_color, DEFAULT_BORDER_COLOR); mBorderOverlay = a.getBoolean(R.styleable.roundedimageview_border_overlay, DEFAULT_BORDER_OVERLAY); a.recycle(); init(); } private void init() { super.setScaleType(SCALE_TYPE); 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 public void setAdjustViewBounds(boolean adjustViewBounds) { if (adjustViewBounds) { throw new IllegalArgumentException("adjustViewBounds not supported."); } } @Override protected void onDraw(Canvas canvas) { if (getDrawable() == null) { return; } canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint); if (mBorderWidth != 0) { 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 void setBorderColorResource(@ColorRes int borderColorRes) { setBorderColor(getContext().getResources().getColor(borderColorRes)); } public int getBorderWidth() { return mBorderWidth; } public void setBorderWidth(int borderWidth) { if (borderWidth == mBorderWidth) { return; } mBorderWidth = borderWidth; setup(); } public boolean isBorderOverlay() { return mBorderOverlay; } public void setBorderOverlay(boolean borderOverlay) { if (borderOverlay == mBorderOverlay) { return; } mBorderOverlay = borderOverlay; 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(@DrawableRes int resId) { super.setImageResource(resId); mBitmap = getBitmapFromDrawable(getDrawable()); setup(); } @Override public void setImageURI(Uri uri) { super.setImageURI(uri); mBitmap = getBitmapFromDrawable(getDrawable()); setup(); } @Override public void setColorFilter(ColorFilter cf) { if (cf == mColorFilter) { return; } mColorFilter = cf; mBitmapPaint.setColorFilter(mColorFilter); invalidate(); } 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(mBorderRect); if (!mBorderOverlay) { mDrawableRect.inset(mBorderWidth, 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) + mDrawableRect.left, (int) (dy + 0.5f) + mDrawableRect.top); mBitmapShader.setLocalMatrix(mShaderMatrix); }}
attrs 部分
<!--圆形头像--><declare-styleable name="roundedimageview"> <!-- 圆形图片 --> <attr name="border_width" format="dimension" /> <attr name="border_color" format="color" /> <attr name="border_overlay" format="boolean" /></declare-styleable> <!--虚化背景--><declare-styleable name="BlurredView"> <attr name="src" format="reference" /> <attr name="move" format="boolean" /> <attr name="disableBlurred" format="boolean" /></declare-styleable>
Blur毛玻璃效果
这个从网上看到的,摘过来
BlurBitmap
/** * 描述: * 作者:Marc on 2016/8/4 09:14 * 邮箱:aliali_ha@yeah.net */public class BlurBitmap { /** * 图片缩放比例 */ private static final float BITMAP_SCALE = 0.4f; /** * 最大模糊度(在0.0到25.0之间) */ private static final float BLUR_RADIUS = 25f; @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public static Bitmap blur(Context context, Bitmap image) { // 计算图片缩小后的长宽 int width = Math.round(image.getWidth() * BITMAP_SCALE); int height = Math.round(image.getHeight() * BITMAP_SCALE); // 将缩小后的图片做为预渲染的图片。 Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false); // 创建一张渲染后的输出图片。 Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap); // 创建RenderScript内核对象 RenderScript rs = RenderScript.create(context); // 创建一个模糊效果的RenderScript的工具对象 ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间。 // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去。 Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap); Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap); // 设置渲染的模糊程度, 25f是最大模糊度 blurScript.setRadius(BLUR_RADIUS); // 设置blurScript对象的输入内存 blurScript.setInput(tmpIn); // 将输出数据保存到输出内存中 blurScript.forEach(tmpOut); // 将数据填充到Allocation中 tmpOut.copyTo(outputBitmap); return outputBitmap; }}
BlurredUtil
/** * 描述: * 作者:Marc on 2016/8/4 10:16 * 邮箱:aliali_ha@yeah.net */public class BlurredUtil { /** * 将Drawable对象转化为Bitmap对象 * * @param drawable Drawable对象 * @return 对应的Bitmap对象 */ static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap; if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if (bitmapDrawable.getBitmap() != null) { return bitmapDrawable.getBitmap(); } } if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel } else { bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; }}
BlurredView
/** * 描述: * 作者:Marc on 2016/8/4 10:16 * 邮箱:aliali_ha@yeah.net */ public class BlurredView extends RelativeLayout{ /** * 模糊最大化值 */ private static final int ALPHA_MAX_VALUE = 255; /** * Context */ private Context mContext; /** * 模糊后的ImageView */ private ImageView mBlurredImg; /** * 原图ImageView */ private ImageView mOriginImg; /** * 原图Bitmap */ private Bitmap mOriginBitmap; /** * 模糊后的Bitmap */ private Bitmap mBlurredBitmap; /** * 是否禁用模糊效果 */ private boolean isDisableBlurred; /** * 是否移动背景图片 */ private boolean isMove; public BlurredView(Context context) { super(context); init(context); } public BlurredView(Context context, AttributeSet attrs) { super(context, attrs); init(context); initAttr(context, attrs); } public BlurredView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); initAttr(context, attrs); } private void init(Context context) { mContext = context; LayoutInflater.from(context).inflate(R.layout.blurredview, this); mOriginImg = (ImageView) findViewById(R.id.blurredview_origin_img); mBlurredImg = (ImageView) findViewById(R.id.blurredview_blurred_img); } private void initAttr(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BlurredView); Drawable drawable = typedArray.getDrawable(R.styleable.BlurredView_src); isMove = typedArray.getBoolean(R.styleable.BlurredView_move, false); isDisableBlurred = typedArray.getBoolean(R.styleable.BlurredView_disableBlurred, false); typedArray.recycle(); // blur image if (null != drawable) { mOriginBitmap = BlurredUtil.drawableToBitmap(drawable); mBlurredBitmap = BlurBitmap.blur(context, mOriginBitmap); } // setVisibility if (!isDisableBlurred) { mBlurredImg.setVisibility(VISIBLE); } // setMove if (null != drawable) { setMove(context, isMove); } } /** * 设置背景图片移动效果 * @param context 上下文对象 * @param isMove 是否移动 */ private void setMove(Context context, boolean isMove) { if (isMove) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point point = new Point(); display.getSize(point); int height = point.y; setBlurredHeight(height, mOriginImg); setBlurredHeight(height, mBlurredImg); } } /** * 改变图片的高度 * * @param height 图片的高度 * @param imageView imageview对象 */ private void setBlurredHeight(int height, ImageView imageView) { ViewGroup.LayoutParams params = imageView.getLayoutParams(); params.width = ViewGroup.LayoutParams.MATCH_PARENT; params.height = height + 100; imageView.requestLayout(); } @Override protected void onFinishInflate() { super.onFinishInflate(); setImageView(); } /** * 填充ImageView. */ private void setImageView() { mBlurredImg.setImageBitmap(mBlurredBitmap); mOriginImg.setImageBitmap(mOriginBitmap); } /** * 以代码的方式添加待模糊的图片 * * @param blurredBitmap 待模糊的图片 */ public void setBlurredImg(Bitmap blurredBitmap) { if (null != blurredBitmap) { mOriginBitmap = blurredBitmap; mBlurredBitmap = BlurBitmap.blur(mContext, blurredBitmap); setImageView(); setMove(mContext, isMove); } } /** * 以代码的方式添加待模糊的图片 * * @param blurDrawable 待模糊的图片 */ public void setBlurredImg(Drawable blurDrawable) { if (null != blurDrawable) { mOriginBitmap = BlurredUtil.drawableToBitmap(blurDrawable); mBlurredBitmap = BlurBitmap.blur(mContext, mOriginBitmap); setImageView(); setMove(mContext, isMove); } } /** * 设置模糊程度 * * @param level 模糊程度, 数值在 0~100 之间. */ public void setBlurredLevel(int level) { if (level < 0 || level > 100) { throw new IllegalStateException("No validate level, the value must be 0~100"); } if (isDisableBlurred) { return; } mOriginImg.setAlpha((int) (ALPHA_MAX_VALUE - level * 2.55)); } /** * 设置图片上移的距离 * * @param hight 向上移动的距离 */ public void setBlurredTop(int hight) { mOriginImg.setTop(-hight); mBlurredImg.setTop(-hight); } /** * 显示模糊图片 */ public void showBlurredView() { mBlurredImg.setVisibility(VISIBLE); } /** * 禁用模糊效果 */ public void disableBlurredView() { isDisableBlurred = true; mOriginImg.setAlpha(ALPHA_MAX_VALUE); mBlurredImg.setVisibility(INVISIBLE); } /** * 启用模糊效果 */ public void enableBlurredView() { isDisableBlurred = false; mBlurredImg.setVisibility(VISIBLE); } }
使用
BlueActivity
/** * 描述: * 作者:Marc on 2016/8/4 10:29 * 邮箱:aliali_ha@yeah.net */public class BlueActivity extends AppCompatActivity { Toolbar toolbar; TextView toobarTv; BlurWeatherFragment fragment1; BlurredViewBasicFragment fragment2; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_blue_main); ButterKnife.bind(this); toolbar = (Toolbar) findViewById(R.id.toolbar); toobarTv = (TextView) findViewById(R.id.toolbar_tv); toobarTv.setText("Blur"); fragment1 = new BlurWeatherFragment(); fragment2 = new BlurredViewBasicFragment(); setDefaultFragment(); } private void setDefaultFragment() { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = manager.beginTransaction(); fragmentTransaction.replace(R.id.container, fragment1); fragmentTransaction.commit(); } @OnClick({R.id.blue_btn1, R.id.blue_btn2}) public void onClick(View view) { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = manager.beginTransaction(); switch (view.getId()) { case R.id.blue_btn1: if (fragment1 == null) { fragment1 = new BlurWeatherFragment(); } fragmentTransaction.replace(R.id.container, fragment1); break; case R.id.blue_btn2: if (fragment2 == null) { fragment2 = new BlurredViewBasicFragment(); } fragmentTransaction.replace(R.id.container, fragment2); break; } fragmentTransaction.commit(); }}
xml 布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/toolbar" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/blue_btn1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="RecycleView样式" /> <Button android:id="@+id/blue_btn2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="调节虚化" /> </LinearLayout> <RelativeLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"></RelativeLayout> </LinearLayout>
BlurredViewBasicFragment
/** * 描述: * 作者:Marc on 2016/8/4 10:54 * 邮箱:aliali_ha@yeah.net */ public class BlurredViewBasicFragment extends Fragment { Activity activity; /** * 进度条SeekBar */ private SeekBar mSeekBar; /** * 显示进度的文字 */ private TextView mProgressTv; /** * Blurredview */ private BlurredView mBlurredView; private View view; @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); activity = getActivity(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = View.inflate(activity, R.layout.blurrediview_basic, null); // 初始化视图 initViews(); // 处理seekbar滑动事件 setSeekBar(); return view; } private void initViews() { mSeekBar = (SeekBar) view.findViewById(R.id.activity_main_seekbar); mProgressTv = (TextView) view.findViewById(R.id.activity_main_progress_tv); mBlurredView = (BlurredView) view.findViewById(R.id.activity_main_blurredview); // 可以在代码中使用setBlurredImg()方法指定需要模糊的图片 mBlurredView.setBlurredImg(getResources().getDrawable(R.drawable.gou)); // 设置完成后需要调用showBlurredView方法显示要模糊的图片 mBlurredView.showBlurredView(); } private void setSeekBar() { mSeekBar.setMax(100); mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { mBlurredView.setBlurredLevel(progress); mProgressTv.setText(String.valueOf(progress)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } }
xml布局
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <com.tescoo.aat.aat.blur.BlurredView android:id="@+id/activity_main_blurredview" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 没有指定圆形ImageView属性时,默认没有外边圆颜色 --> <!-- 需要将图片资源自定为src ,或在程序中setImageResource(res) 不能设置background为图片,这样不能达到圆形效果--> <com.tescoo.aat.widget.RoundImageView android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:src="@drawable/gou" app:border_color="#10FFFFFF" app:border_width="5dp" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="80dp" android:orientation="vertical"> <SeekBar android:id="@+id/activity_main_seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginTop="@dimen/activity_vertical_margin" /> <TextView android:id="@+id/activity_main_progress_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="0" android:textSize="24sp" /> </LinearLayout> </LinearLayout>
BlurRecycleFragment
/** * 描述: * 作者:Marc on 2016/8/4 10:44 * 邮箱:aliali_ha@yeah.net */ public class BlurRecycleFragment extends Fragment { /** * blurredview */ private BlurredView mBlurredView; /** * RecyclerView */ private RecyclerView mRecyclerView; private int mScrollerY; private int mAlpha; Activity activity; @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); activity = getActivity(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = View.inflate(activity, R.layout.blurrediview_weather, null); mBlurredView = (BlurredView) view.findViewById(R.id.yahooweather_blurredview); mRecyclerView = (RecyclerView) view.findViewById(R.id.yahooweather_recyclerview); mRecyclerView.setLayoutManager(new LinearLayoutManager(activity)); mRecyclerView.setAdapter(new RecyclerViewAdapter(activity)); mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); mScrollerY += dy; if (Math.abs(mScrollerY) > 1000) { mBlurredView.setBlurredTop(100); mAlpha = 100; } else { mBlurredView.setBlurredTop(mScrollerY / 10); mAlpha = Math.abs(mScrollerY) / 10; } mBlurredView.setBlurredLevel(mAlpha); } }); return view; } }
xml布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.tescoo.aat.aat.blur.BlurredView android:id="@+id/recycleblur_blurredview" android:layout_width="match_parent" android:layout_height="match_parent" app:disableBlurred="false" app:move="true" app:src="@drawable/gou" /> <LinearLayout android:id="@+id/top" android:background="@drawable/title_shape" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingBottom="8dp" android:paddingTop="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Marc" android:textColor="@android:color/white" android:textSize="18sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="aliali_ha" android:textColor="@android:color/white" android:textSize="10sp" /> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/blur_recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/top" /> </RelativeLayout>
RecyclerViewAdapter
/** * 描述:这里注意。使用了2个ViewHolder,类型使用RecyclerView.ViewHolder * 作者:Marc on 2016/8/4 14:27 * 邮箱:aliali_ha@yeah.net */public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private final Context context; private static final int ITEM_COUNT = 10; private static final int TYPE_HEADER = 0; private static final int TYPE_ITEM = 1; public RecyclerViewAdapter(Context context) { this.context = context; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == TYPE_HEADER) { return new HeaderHolder(LayoutInflater.from(context).inflate(R.layout.recyclerview_header, parent, false)); } return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.recyclerview_item, parent, false)); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { //这里没使用数据 } @Override public int getItemCount() { return ITEM_COUNT; } /** * recycleview增加header什么的感觉确实比listview方便了好多 * * @param position * @return */ @Override public int getItemViewType(int position) { if (position == 0) { return TYPE_HEADER; } else { return TYPE_ITEM; } } /** * item的viewholder */ public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } /** * 头部viewholder */ private class HeaderHolder extends RecyclerView.ViewHolder { public HeaderHolder(View itemView) { super(itemView); } }}
0 0
- 圆形imageview以及Blur
- 圆形imageview
- 圆形ImageView
- 圆形ImageView
- 圆形imageView
- 圆形ImageView
- 圆形ImageView
- 圆形ImageView
- 圆形头像以及一些常见需求形状自定义ImageView组件
- android圆形的ImageView
- android圆形的ImageView
- Android圆形的ImageView
- imageView创建圆形头像
- Android 圆形 ImageView
- imageview显示圆形图片
- android 圆形的ImageView
- 重写ImageView圆形边框
- android:圆形 ImageView
- Linux Completion
- 运维工具pssh mussh
- 泛型通配符中,子类边界与父类边界方法受限原因
- Java自动装箱和拆箱
- 因特网路由选择协议之RIP
- 圆形imageview以及Blur
- Hyperledger -超级账本 学习报告
- JS基础之运动框架,能实现常用动画效果
- vmware安装mac的一些问题
- 232. Implement Queue using Stacks
- zookeeper常用命令
- Android热修复三部曲之动态加载补丁.dex文件
- python divmod()函数
- redis-cluster 集群搭建