闲来麻将可以修改头像棋牌,怎么上传自己的头像

闲来无事,把自己的头像画下_霸州吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:160,409贴子:
闲来无事,把自己的头像画下收藏
不喜欢的勿喷啊…画好更新
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或手机号/邮箱:?
使用第三方登录:?
还可以输入 300 字符
请输入举报理由
选择收藏分类:
默认收藏夹
添加收藏分类:
原创作品:闲来没事做了个头像,可惜不像我,像个人就行
系统分类:
作品版权由
解释,禁止匿名转载;禁止商业使用;禁止个人使用。 临摹作品,同人作品原型版权归原作者所有。
上海市 / GUI设计师5年前发布
有点小囧的头像
上海市 / GUI设计师5年前发布
同时推荐此作品
还可以输入2000 个字符
所有,禁止匿名转载;禁止商业使用;禁止个人使用。镜花水月blog(64)
大晚上的,夜深人静!闲来无聊!决定再玩一玩头像上传,众所周知,app的个人资料那一页具有更换头像,上传头像的功能,当然,这已经不是什么新鲜事了~有些人就是不喜欢应用自带的那种默认头像,有些其丑无比,就想换成自己喜欢的头像!可以从相册中选图片,也可以调用相机来美美的摆个pose,伸出剪刀手~自拍一张再上传也可以~
本篇博客实现的功能就是点击头像,然后可以换头像!例子中的头像为圆形头像,点击头像后,会从底部弹出一个PopupWindow,然后选择相机或者相册都可以,然后设置头像就行了~当然显示的也是圆形的!ok!不啰嗦了!
package com.example.lenovo.demo.
import android.app.A
import android.content.I
import android.graphics.B
import android.net.U
import android.os.B
import android.os.E
import android.provider.MediaS
import android.view.G
import android.view.V
import android.view.ViewG
import android.widget.B
import android.widget.ImageV
import android.widget.PopupW
import android.widget.T
import com.example.lenovo.demo.R;
import java.io.F
public class MainActivity extends Activity implements View.OnClickListener{
private ImageView head_portrait_
private Button camera,album,
private PopupWindow popupW
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
icon = (ImageView) findViewById(R.id.icon);
icon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showPopupWindow();
* 显示弹窗
private void showPopupWindow() {
View popupView = View.inflate(MainActivity.this,R.layout.popup_view,null);
camera = (Button) popupView.findViewById(R.id.camera);
album = (Button) popupView.findViewById(R.id.album);
cancel = (Button) popupView.findViewById(R.id.cancel);
camera.setOnClickListener(this);
album.setOnClickListener(this);
cancel.setOnClickListener(this);
popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(false);
popupWindow.setAnimationStyle(android.R.style.Animation_InputMethod);
popupWindow.showAtLocation(popupView, Gravity.BOTTOM,0,0);
public void onClick(View v) {
switch (v.getId()){
case R.id.camera:
if (!issdkard())break;
openCamera();
case R.id.album:
openAlbum();
case R.id.cancel:
popupWindow.dismiss();
* 拍照作为头像
public void openCamera() {
imgFile = new File(destDir, getPhotoFileName());
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgFile));
startActivityForResult(intent, 1);
public static String SDPATH = Environment.getExternalStorageDirectory() + "/hsyfm/recordimg/";
public static File destDir = new File(SDPATH);
private File imgF
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("'PNG'_yyyyMMdd_HHmmss");
return sdf.format(date) + ".png";
* 判断SD卡是否存在
private boolean issdkard() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
if (!destDir.exists()) {
destDir.mkdirs();
return true;
showToastShort("SD卡不存在");
return false;
* 相册中选择图片作为头像
public void openAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, 2);
* Intent请求回调函数
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
if (imgFile.exists()) {
startPhotoZoom(Uri.fromFile(imgFile), 300);
if (null != data) {
startPhotoZoom(data.getData(), 300);
if (null != data) {
Bundle bundle = data.getExtras();
final Bitmap bmp = bundle.getParcelable("data");
saveCropPic(bmp);
head_portrait_icon.setImageBitmap(bmp);
* 图片进行裁剪
private void startPhotoZoom(Uri uri, int size) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", size);
intent.putExtra("outputY", size);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3);
private void saveCropPic(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileOutputStream fis = null;
pressFormat.PNG, 100, baos);
fis = new FileOutputStream(imgFile);
fis.write(baos.toByteArray());
fis.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != baos) {
baos.close();
if (null != fis) {
fis.close();
} catch (IOException e) {
e.printStackTrace();
upImgFile();
* 上传图片到服务器
private void upImgFile() {
至于弹窗的xml布局显示,相信你们都会,就不贴了!CircleImageView呢!网上也有 so many 例子!OK 结束!!算了,还是贴一下圆形的ImageView吧!
—————————————————————————————————————————————————————————————————
首先是自定义属性:
&?xml version="1.0" encoding="utf-8"?&
name="CircleImageView"&
name="riv_corner_radius" format="dimension" /&
name="riv_corner_radius_top_left" format="dimension" /&
name="riv_corner_radius_top_right" format="dimension" /&
name="riv_corner_radius_bottom_left" format="dimension" /&
name="riv_corner_radius_bottom_right" format="dimension" /&
name="riv_border_width" format="dimension" /&
name="riv_border_color" format="color" /&
name="riv_mutate_background" format="boolean" /&
name="riv_oval" format="boolean" /&
name="android:scaleType" /&
name="riv_tile_mode"&
name="clamp" value="0" /&
name="repeat" value="1" /&
name="mirror" value="2" /&
name="riv_tile_mode_x"&
name="clamp" value="0" /&
name="repeat" value="1" /&
name="mirror" value="2" /&
name="riv_tile_mode_y"&
name="clamp" value="0" /&
name="repeat" value="1" /&
name="mirror" value="2" /&
然后是3个相关类
package com.mirrorflower.idolfans.view
import android.support.annotation.IntDef
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
@Retention(RetentionPolicy.SOURCE)
Corner.TOP_LEFT, Corner.TOP_RIGHT,
Corner.BOTTOM_LEFT, Corner.BOTTOM_RIGHT
public @interface Corner {
int TOP_LEFT = 0
int TOP_RIGHT = 1
int BOTTOM_RIGHT = 2
int BOTTOM_LEFT = 3
package com.mirrorflower.idolfans.
import android.content.res.ColorStateL
import android.graphics.B
import android.graphics.Bitmap.C
import android.graphics.BitmapS
import android.graphics.C
import android.graphics.C
import android.graphics.ColorF
import android.graphics.M
import android.graphics.P
import android.graphics.PixelF
import android.graphics.R
import android.graphics.RectF;
import android.graphics.S
import android.graphics.drawable.BitmapD
import android.graphics.drawable.D
import android.graphics.drawable.LayerD
import android.support.annotation.ColorI
import android.support.annotation.NonN
import android.util.L
import android.widget.ImageView.ScaleT
import java.util.HashS
import java.util.S
@SuppressWarnings("UnusedDeclaration")
public class RoundedDrawable extends Drawable {
public static final String TAG = "RoundedDrawable";
public static final int DEFAULT_BORDER_COLOR = Color.BLACK;
private final RectF mBounds = new RectF();
private final RectF mDrawableRect = new RectF();
private final RectF mBitmapRect = new RectF();
private final Bitmap mB
private final Paint mBitmapP
private final int mBitmapW
private final int mBitmapH
private final RectF mBorderRect = new RectF();
private final Paint mBorderP
private final Matrix mShaderMatrix = new Matrix();
private final RectF mSquareCornersRect = new RectF();
private Shader.TileMode mTileModeX = Shader.TileMode.CLAMP;
private Shader.TileMode mTileModeY = Shader.TileMode.CLAMP;
private boolean mRebuildShader = true;
private float mCornerRadius = 0f;
private final boolean[] mCornersRounded = new boolean[] { true, true, true, true };
private boolean mOval = false;
private float mBorderWidth = 0;
private ColorStateList mBorderColor = ColorStateList.valueOf(DEFAULT_BORDER_COLOR);
private ScaleType mScaleType = ScaleType.FIT_CENTER;
public RoundedDrawable(Bitmap bitmap) {
mBitmapWidth = bitmap.getWidth();
mBitmapHeight = bitmap.getHeight();
mBitmapRect.set(0, 0, mBitmapWidth, mBitmapHeight);
mBitmapPaint = new Paint();
mBitmapPaint.setStyle(Paint.Style.FILL);
mBitmapPaint.setAntiAlias(true);
mBorderPaint = new Paint();
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setAntiAlias(true);
mBorderPaint.setColor(mBorderColor.getColorForState(getState(), DEFAULT_BORDER_COLOR));
mBorderPaint.setStrokeWidth(mBorderWidth);
public static RoundedDrawable fromBitmap(Bitmap bitmap) {
if (bitmap != null) {
return new RoundedDrawable(bitmap);
return null;
public static Drawable fromDrawable(Drawable drawable) {
if (drawable != null) {
if (drawable instanceof RoundedDrawable) {
} else if (drawable instanceof LayerDrawable) {
LayerDrawable ld = (LayerDrawable)
int num = ld.getNumberOfLayers();
for (int i = 0; i & i++) {
Drawable d = ld.getDrawable(i);
ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d));
Bitmap bm = drawableToBitmap(drawable);
if (bm != null) {
return new RoundedDrawable(bm);
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
int width = Math.max(drawable.getIntrinsicWidth(), 2);
int height = Math.max(drawable.getIntrinsicHeight(), 2);
bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
} catch (Exception e) {
e.printStackTrace();
Log.w(TAG, "Failed to create bitmap from drawable!");
bitmap = null;
public Bitmap getSourceBitmap() {
public boolean isStateful() {
return mBorderColor.isStateful();
protected boolean onStateChange(int[] state) {
int newColor = mBorderColor.getColorForState(state, 0);
if (mBorderPaint.getColor() != newColor) {
mBorderPaint.setColor(newColor);
return true;
return super.onStateChange(state);
private void updateShaderMatrix() {
switch (mScaleType) {
case CENTER:
mBorderRect.set(mBounds);
mBorderRect.inset(mBorderWidth / 2, mBorderWidth / 2);
mShaderMatrix.reset();
mShaderMatrix.setTranslate((int) ((mBorderRect.width() - mBitmapWidth) * 0.5f + 0.5f),
(int) ((mBorderRect.height() - mBitmapHeight) * 0.5f + 0.5f));
case CENTER_CROP:
mBorderRect.set(mBounds);
mBorderRect.inset(mBorderWidth / 2, mBorderWidth / 2);
mShaderMatrix.reset();
if (mBitmapWidth * mBorderRect.height() & mBorderRect.width() * mBitmapHeight) {
scale = mBorderRect.height() / (float) mBitmapH
dx = (mBorderRect.width() - mBitmapWidth * scale) * 0.5f;
scale = mBorderRect.width() / (float) mBitmapW
dy = (mBorderRect.height() - mBitmapHeight * scale) * 0.5f;
mShaderMatrix.setScale(scale, scale);
mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth / 2,
(int) (dy + 0.5f) + mBorderWidth / 2);
case CENTER_INSIDE:
mShaderMatrix.reset();
if (mBitmapWidth &= mBounds.width() && mBitmapHeight &= mBounds.height()) {
scale = 1.0f;
scale = Math.min(mBounds.width() / (float) mBitmapWidth,
mBounds.height() / (float) mBitmapHeight);
dx = (int) ((mBounds.width() - mBitmapWidth * scale) * 0.5f + 0.5f);
dy = (int) ((mBounds.height() - mBitmapHeight * scale) * 0.5f + 0.5f);
mShaderMatrix.setScale(scale, scale);
mShaderMatrix.postTranslate(dx, dy);
mBorderRect.set(mBitmapRect);
mShaderMatrix.mapRect(mBorderRect);
mBorderRect.inset(mBorderWidth / 2, mBorderWidth / 2);
mShaderMatrix.setRectToRect(mBitmapRect, mBorderRect, Matrix.ScaleToFit.FILL);
case FIT_CENTER:
mBorderRect.set(mBitmapRect);
mShaderMatrix.setRectToRect(mBitmapRect, mBounds, Matrix.ScaleToFit.CENTER);
mShaderMatrix.mapRect(mBorderRect);
mBorderRect.inset(mBorderWidth / 2, mBorderWidth / 2);
mShaderMatrix.setRectToRect(mBitmapRect, mBorderRect, Matrix.ScaleToFit.FILL);
case FIT_END:
mBorderRect.set(mBitmapRect);
mShaderMatrix.setRectToRect(mBitmapRect, mBounds, Matrix.ScaleToFit.END);
mShaderMatrix.mapRect(mBorderRect);
mBorderRect.inset(mBorderWidth / 2, mBorderWidth / 2);
mShaderMatrix.setRectToRect(mBitmapRect, mBorderRect, Matrix.ScaleToFit.FILL);
case FIT_START:
mBorderRect.set(mBitmapRect);
mShaderMatrix.setRectToRect(mBitmapRect, mBounds, Matrix.ScaleToFit.START);
mShaderMatrix.mapRect(mBorderRect);
mBorderRect.inset(mBorderWidth / 2, mBorderWidth / 2);
mShaderMatrix.setRectToRect(mBitmapRect, mBorderRect, Matrix.ScaleToFit.FILL);
case FIT_XY:
mBorderRect.set(mBounds);
mBorderRect.inset(mBorderWidth / 2, mBorderWidth / 2);
mShaderMatrix.reset();
mShaderMatrix.setRectToRect(mBitmapRect, mBorderRect, Matrix.ScaleToFit.FILL);
mDrawableRect.set(mBorderRect);
protected void onBoundsChange(@NonNull Rect bounds) {
super.onBoundsChange(bounds);
mBounds.set(bounds);
updateShaderMatrix();
public void draw(@NonNull Canvas canvas) {
if (mRebuildShader) {
BitmapShader bitmapShader = new BitmapShader(mBitmap, mTileModeX, mTileModeY);
if (mTileModeX == Shader.TileMode.CLAMP && mTileModeY == Shader.TileMode.CLAMP) {
bitmapShader.setLocalMatrix(mShaderMatrix);
mBitmapPaint.setShader(bitmapShader);
mRebuildShader = false;
if (mOval) {
if (mBorderWidth & 0) {
canvas.drawOval(mDrawableRect, mBitmapPaint);
canvas.drawOval(mBorderRect, mBorderPaint);
canvas.drawOval(mDrawableRect, mBitmapPaint);
if (any(mCornersRounded)) {
float radius = mCornerR
if (mBorderWidth & 0) {
canvas.drawRoundRect(mDrawableRect, radius, radius, mBitmapPaint);
canvas.drawRoundRect(mBorderRect, radius, radius, mBorderPaint);
redrawBitmapForSquareCorners(canvas);
redrawBorderForSquareCorners(canvas);
canvas.drawRoundRect(mDrawableRect, radius, radius, mBitmapPaint);
redrawBitmapForSquareCorners(canvas);
canvas.drawRect(mDrawableRect, mBitmapPaint);
if (mBorderWidth & 0) {
canvas.drawRect(mBorderRect, mBorderPaint);
private void redrawBitmapForSquareCorners(Canvas canvas) {
if (all(mCornersRounded)) {
if (mCornerRadius == 0) {
float left = mDrawableRect.
float top = mDrawableRect.
float right = left + mDrawableRect.width();
float bottom = top + mDrawableRect.height();
float radius = mCornerR
if (!mCornersRounded[Corner.TOP_LEFT]) {
mSquareCornersRect.set(left, top, left + radius, top + radius);
canvas.drawRect(mSquareCornersRect, mBitmapPaint);
if (!mCornersRounded[Corner.TOP_RIGHT]) {
mSquareCornersRect.set(right - radius, top, right, radius);
canvas.drawRect(mSquareCornersRect, mBitmapPaint);
if (!mCornersRounded[Corner.BOTTOM_RIGHT]) {
mSquareCornersRect.set(right - radius, bottom - radius, right, bottom);
canvas.drawRect(mSquareCornersRect, mBitmapPaint);
if (!mCornersRounded[Corner.BOTTOM_LEFT]) {
mSquareCornersRect.set(left, bottom - radius, left + radius, bottom);
canvas.drawRect(mSquareCornersRect, mBitmapPaint);
private void redrawBorderForSquareCorners(Canvas canvas) {
if (all(mCornersRounded)) {
if (mCornerRadius == 0) {
float left = mDrawableRect.
float top = mDrawableRect.
float right = left + mDrawableRect.width();
float bottom = top + mDrawableRect.height();
float radius = mCornerR
float offset = mBorderWidth / 2;
if (!mCornersRounded[Corner.TOP_LEFT]) {
canvas.drawLine(left - offset, top, left + radius, top, mBorderPaint);
canvas.drawLine(left, top - offset, left, top + radius, mBorderPaint);
if (!mCornersRounded[Corner.TOP_RIGHT]) {
canvas.drawLine(right - radius - offset, top, right, top, mBorderPaint);
canvas.drawLine(right, top - offset, right, top + radius, mBorderPaint);
if (!mCornersRounded[Corner.BOTTOM_RIGHT]) {
canvas.drawLine(right - radius - offset, bottom, right + offset, bottom, mBorderPaint);
canvas.drawLine(right, bottom - radius, right, bottom, mBorderPaint);
if (!mCornersRounded[Corner.BOTTOM_LEFT]) {
canvas.drawLine(left - offset, bottom, left + radius, bottom, mBorderPaint);
canvas.drawLine(left, bottom - radius, left, bottom, mBorderPaint);
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
public int getAlpha() {
return mBitmapPaint.getAlpha();
public void setAlpha(int alpha) {
mBitmapPaint.setAlpha(alpha);
invalidateSelf();
public ColorFilter getColorFilter() {
return mBitmapPaint.getColorFilter();
public void setColorFilter(ColorFilter cf) {
mBitmapPaint.setColorFilter(cf);
invalidateSelf();
public void setDither(boolean dither) {
mBitmapPaint.setDither(dither);
invalidateSelf();
public void setFilterBitmap(boolean filter) {
mBitmapPaint.setFilterBitmap(filter);
invalidateSelf();
public int getIntrinsicWidth() {
return mBitmapW
public int getIntrinsicHeight() {
return mBitmapH
* the corner radius.
public float getCornerRadius() {
return mCornerR
* corner the specific corner to get radius of.
* the corner radius of the specified corner.
public float getCornerRadius(@Corner int corner) {
return mCornersRounded[corner] ? mCornerRadius : 0f;
* Sets all corners to the specified radius.
* radius the radius.
* the {@link RoundedDrawable} for chaining.
public RoundedDrawable setCornerRadius(float radius) {
setCornerRadius(radius, radius, radius, radius);
return this;
* Sets the corner radius of one specific corner.
* corner the corner.
* radius the radius.
* the {@link RoundedDrawable} for chaining.
public RoundedDrawable setCornerRadius(@Corner int corner, float radius) {
if (radius != 0 && mCornerRadius != 0 && mCornerRadius != radius) {
throw new IllegalArgumentException("Multiple nonzero corner radii not yet supported.");
if (radius == 0) {
if (only(corner, mCornersRounded)) {
mCornerRadius = 0;
mCornersRounded[corner] = false;
if (mCornerRadius == 0) {
mCornerRadius =
mCornersRounded[corner] = true;
return this;
* Sets the corner radii of all the corners.
* topLeft top left corner radius.
* topRight top right corner radius
* bottomRight bototm right corner radius.
* bottomLeft bottom left corner radius.
* the {@link RoundedDrawable} for chaining.
public RoundedDrawable setCornerRadius(float topLeft, float topRight, float bottomRight,
float bottomLeft) {
Set&Float& radiusSet = new HashSet&&(4);
radiusSet.add(topLeft);
radiusSet.add(topRight);
radiusSet.add(bottomRight);
radiusSet.add(bottomLeft);
radiusSet.remove(0f);
if (radiusSet.size() & 1) {
throw new IllegalArgumentException("Multiple nonzero corner radii not yet supported.");
if (!radiusSet.isEmpty()) {
float radius = radiusSet.iterator().next();
if (Float.isInfinite(radius) || Float.isNaN(radius) || radius & 0) {
throw new IllegalArgumentException("Invalid radius value: " + radius);
mCornerRadius =
mCornerRadius = 0f;
mCornersRounded[Corner.TOP_LEFT] = topLeft & 0;
mCornersRounded[Corner.TOP_RIGHT] = topRight & 0;
mCornersRounded[Corner.BOTTOM_RIGHT] = bottomRight & 0;
mCornersRounded[Corner.BOTTOM_LEFT] = bottomLeft & 0;
return this;
public float getBorderWidth() {
return mBorderW
public RoundedDrawable setBorderWidth(float width) {
mBorderWidth =
mBorderPaint.setStrokeWidth(mBorderWidth);
return this;
public int getBorderColor() {
return mBorderColor.getDefaultColor();
public RoundedDrawable setBorderColor(@ColorInt int color) {
return setBorderColor(ColorStateList.valueOf(color));
public ColorStateList getBorderColors() {
return mBorderC
public RoundedDrawable setBorderColor(ColorStateList colors) {
mBorderColor = colors != null ? colors : ColorStateList.valueOf(0);
mBorderPaint.setColor(mBorderColor.getColorForState(getState(), DEFAULT_BORDER_COLOR));
return this;
public boolean isOval() {
public RoundedDrawable setOval(boolean oval) {
return this;
public ScaleType getScaleType() {
return mScaleT
public RoundedDrawable setScaleType(ScaleType scaleType) {
if (scaleType == null) {
scaleType = ScaleType.FIT_CENTER;
if (mScaleType != scaleType) {
mScaleType = scaleT
updateShaderMatrix();
return this;
public Shader.TileMode getTileModeX() {
return mTileModeX;
public RoundedDrawable setTileModeX(Shader.TileMode tileModeX) {
if (mTileModeX != tileModeX) {
mTileModeX = tileModeX;
mRebuildShader = true;
invalidateSelf();
return this;
public Shader.TileMode getTileModeY() {
return mTileModeY;
public RoundedDrawable setTileModeY(Shader.TileMode tileModeY) {
if (mTileModeY != tileModeY) {
mTileModeY = tileModeY;
mRebuildShader = true;
invalidateSelf();
return this;
private static boolean only(int index, boolean[] booleans) {
for (int i = 0, len = booleans. i & i++) {
if (booleans[i] != (i == index)) {
return false;
return true;
private static boolean any(boolean[] booleans) {
for (boolean b : booleans) {
if (b) { return true; }
return false;
private static boolean all(boolean[] booleans) {
for (boolean b : booleans) {
if (b) { return false; }
return true;
public Bitmap toBitmap() {
return drawableToBitmap(this);
package com.mirrorflower.idolfans.
import android.content.C
import android.content.res.ColorStateL
import android.content.res.R
import android.content.res.TypedA
import android.graphics.B
import android.graphics.ColorF
import android.graphics.S
import android.graphics.drawable.ColorD
import android.graphics.drawable.D
import android.graphics.drawable.LayerD
import android.net.U
import android.support.annotation.ColorI
import android.support.annotation.DimenR
import android.support.annotation.DrawableR
import android.util.AttributeS
import android.util.L
import android.widget.ImageV
import com.mirrorflower.meituan.R;
@SuppressWarnings("UnusedDeclaration")
public class CircleImageView extends ImageView {
private static final int TILE_MODE_UNDEFINED = -2;
private static final int TILE_MODE_CLAMP = 0;
private static final int TILE_MODE_REPEAT = 1;
private static final int TILE_MODE_MIRROR = 2;
public static final String TAG = "RoundedImageView";
public static final float DEFAULT_RADIUS = 0f;
public static final float DEFAULT_BORDER_WIDTH = 0f;
public static final Shader.TileMode DEFAULT_TILE_MODE = Shader.TileMode.CLAMP;
private static final ScaleType[] SCALE_TYPES = {
ScaleType.MATRIX,
ScaleType.FIT_XY,
ScaleType.FIT_START,
ScaleType.FIT_CENTER,
ScaleType.FIT_END,
ScaleType.CENTER,
ScaleType.CENTER_CROP,
ScaleType.CENTER_INSIDE
private final float[] mCornerRadii =
new float[] { DEFAULT_RADIUS, DEFAULT_RADIUS, DEFAULT_RADIUS, DEFAULT_RADIUS };
private Drawable mBackgroundD
private ColorStateList mBorderColor =
ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
private float mBorderWidth = DEFAULT_BORDER_WIDTH;
private ColorFilter mColorFilter = null;
private boolean mColorMod = false;
private Drawable mD
private boolean mHasColorFilter = false;
private boolean mIsOval = false;
private boolean mMutateBackground = false;
private int mR
private int mBackgroundR
private ScaleType mScaleType = ScaleType.FIT_CENTER;
private Shader.TileMode mTileModeX = DEFAULT_TILE_MODE;
private Shader.TileMode mTileModeY = DEFAULT_TILE_MODE;
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);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0);
int index = a.getInt(0, -1);
if (index &= 0) {
setScaleType(SCALE_TYPES[index]);
setScaleType(ScaleType.FIT_CENTER);
float cornerRadiusOverride =
a.getDimensionPixelSize(R.styleable.CircleImageView_riv_corner_radius, -1);
mCornerRadii[Corner.TOP_LEFT] =
a.getDimensionPixelSize(R.styleable.CircleImageView_riv_corner_radius_top_left, -1);
mCornerRadii[Corner.TOP_RIGHT] =
a.getDimensionPixelSize(R.styleable.CircleImageView_riv_corner_radius_top_right, -1);
mCornerRadii[Corner.BOTTOM_RIGHT] =
a.getDimensionPixelSize(R.styleable.CircleImageView_riv_corner_radius_bottom_right, -1);
mCornerRadii[Corner.BOTTOM_LEFT] =
a.getDimensionPixelSize(R.styleable.CircleImageView_riv_corner_radius_bottom_left, -1);
boolean any = false;
for (int i = 0, len = mCornerRadii. i & i++) {
if (mCornerRadii[i] & 0) {
mCornerRadii[i] = 0f;
any = true;
if (!any) {
if (cornerRadiusOverride & 0) {
cornerRadiusOverride = DEFAULT_RADIUS;
for (int i = 0, len = mCornerRadii. i & i++) {
mCornerRadii[i] = cornerRadiusO
mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_riv_border_width, -1);
if (mBorderWidth & 0) {
mBorderWidth = DEFAULT_BORDER_WIDTH;
mBorderColor = a.getColorStateList(R.styleable.CircleImageView_riv_border_color);
if (mBorderColor == null) {
mBorderColor = ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
mMutateBackground = a.getBoolean(R.styleable.CircleImageView_riv_mutate_background, false);
mIsOval = a.getBoolean(R.styleable.CircleImageView_riv_oval, false);
final int tileMode = a.getInt(R.styleable.CircleImageView_riv_tile_mode, TILE_MODE_UNDEFINED);
if (tileMode != TILE_MODE_UNDEFINED) {
setTileModeX(parseTileMode(tileMode));
setTileModeY(parseTileMode(tileMode));
final int tileModeX =
a.getInt(R.styleable.CircleImageView_riv_tile_mode_x, TILE_MODE_UNDEFINED);
if (tileModeX != TILE_MODE_UNDEFINED) {
setTileModeX(parseTileMode(tileModeX));
final int tileModeY =
a.getInt(R.styleable.CircleImageView_riv_tile_mode_y, TILE_MODE_UNDEFINED);
if (tileModeY != TILE_MODE_UNDEFINED) {
setTileModeY(parseTileMode(tileModeY));
updateDrawableAttrs();
updateBackgroundDrawableAttrs(true);
if (mMutateBackground) {
super.setBackgroundDrawable(mBackgroundDrawable);
a.recycle();
private static Shader.TileMode parseTileMode(int tileMode) {
switch (tileMode) {
case TILE_MODE_CLAMP:
return Shader.TileMode.CLAMP;
case TILE_MODE_REPEAT:
return Shader.TileMode.REPEAT;
case TILE_MODE_MIRROR:
return Shader.TileMode.MIRROR;
return null;
protected void drawableStateChanged() {
super.drawableStateChanged();
invalidate();
public ScaleType getScaleType() {
return mScaleT
public void setScaleType(ScaleType scaleType) {
assert scaleType != null;
if (mScaleType != scaleType) {
mScaleType = scaleT
switch (scaleType) {
case CENTER:
case CENTER_CROP:
case CENTER_INSIDE:
case FIT_CENTER:
case FIT_START:
case FIT_END:
case FIT_XY:
super.setScaleType(ScaleType.FIT_XY);
super.setScaleType(scaleType);
updateDrawableAttrs();
updateBackgroundDrawableAttrs(false);
invalidate();
public void setImageDrawable(Drawable drawable) {
mResource = 0;
mDrawable = RoundedDrawable.fromDrawable(drawable);
updateDrawableAttrs();
super.setImageDrawable(mDrawable);
public void setImageBitmap(Bitmap bm) {
mResource = 0;
mDrawable = RoundedDrawable.fromBitmap(bm);
updateDrawableAttrs();
super.setImageDrawable(mDrawable);
public void setImageResource(@DrawableRes int resId) {
if (mResource != resId) {
mResource = resId;
mDrawable = resolveResource();
updateDrawableAttrs();
super.setImageDrawable(mDrawable);
@Override public void setImageURI(Uri uri) {
super.setImageURI(uri);
setImageDrawable(getDrawable());
private Drawable resolveResource() {
Resources rsrc = getResources();
if (rsrc == null) { return null; }
Drawable d = null;
if (mResource != 0) {
d = rsrc.getDrawable(mResource);
} catch (Exception e) {
Log.w(TAG, "Unable to find resource: " + mResource, e);
mResource = 0;
return RoundedDrawable.fromDrawable(d);
public void setBackground(Drawable background) {
setBackgroundDrawable(background);
public void setBackgroundResource(@DrawableRes int resId) {
if (mBackgroundResource != resId) {
mBackgroundResource = resId;
mBackgroundDrawable = resolveBackgroundResource();
setBackgroundDrawable(mBackgroundDrawable);
public void setBackgroundColor(int color) {
mBackgroundDrawable = new ColorDrawable(color);
setBackgroundDrawable(mBackgroundDrawable);
private Drawable resolveBackgroundResource() {
Resources rsrc = getResources();
if (rsrc == null) { return null; }
Drawable d = null;
if (mBackgroundResource != 0) {
d = rsrc.getDrawable(mBackgroundResource);
} catch (Exception e) {
Log.w(TAG, "Unable to find resource: " + mBackgroundResource, e);
mBackgroundResource = 0;
return RoundedDrawable.fromDrawable(d);
private void updateDrawableAttrs() {
updateAttrs(mDrawable, mScaleType);
private void updateBackgroundDrawableAttrs(boolean convert) {
if (mMutateBackground) {
if (convert) {
mBackgroundDrawable = RoundedDrawable.fromDrawable(mBackgroundDrawable);
updateAttrs(mBackgroundDrawable, ScaleType.FIT_XY);
@Override public void setColorFilter(ColorFilter cf) {
if (mColorFilter != cf) {
mColorFilter =
mHasColorFilter = true;
mColorMod = true;
applyColorMod();
invalidate();
private void applyColorMod() {
if (mDrawable != null && mColorMod) {
mDrawable = mDrawable.mutate();
if (mHasColorFilter) {
mDrawable.setColorFilter(mColorFilter);
private void updateAttrs(Drawable drawable, ScaleType scaleType) {
if (drawable == null) { return; }
if (drawable instanceof RoundedDrawable) {
((RoundedDrawable) drawable)
.setScaleType(scaleType)
.setBorderWidth(mBorderWidth)
.setBorderColor(mBorderColor)
.setOval(mIsOval)
.setTileModeX(mTileModeX)
.setTileModeY(mTileModeY);
if (mCornerRadii != null) {
((RoundedDrawable) drawable).setCornerRadius(
mCornerRadii[Corner.TOP_LEFT],
mCornerRadii[Corner.TOP_RIGHT],
mCornerRadii[Corner.BOTTOM_RIGHT],
mCornerRadii[Corner.BOTTOM_LEFT]);
applyColorMod();
} else if (drawable instanceof LayerDrawable) {
LayerDrawable ld = ((LayerDrawable) drawable);
for (int i = 0, layers = ld.getNumberOfLayers(); i & i++) {
updateAttrs(ld.getDrawable(i), scaleType);
@Deprecated
public void setBackgroundDrawable(Drawable background) {
mBackgroundDrawable =
updateBackgroundDrawableAttrs(true);
super.setBackgroundDrawable(mBackgroundDrawable);
* the largest corner radius.
public float getCornerRadius() {
return getMaxCornerRadius();
* the largest corner radius.
public float getMaxCornerRadius() {
float maxRadius = 0;
for (float r : mCornerRadii) {
maxRadius = Math.max(r, maxRadius);
return maxR
* Get the corner radius of a specified corner.
* corner the corner.
* the radius.
public float getCornerRadius(@Corner int corner) {
return mCornerRadii[corner];
* Set all the corner radii from a dimension resource id.
* resId dimension resource id of radii.
public void setCornerRadiusDimen(@DimenRes int resId) {
float radius = getResources().getDimension(resId);
setCornerRadius(radius, radius, radius, radius);
* Set the corner radius of a specific corner from a dimension resource id.
* corner the corner to set.
* resId the dimension resource id of the corner radius.
public void setCornerRadiusDimen(@Corner int corner, @DimenRes int resId) {
setCornerRadius(corner, getResources().getDimensionPixelSize(resId));
* Set the corner radii of all corners in px.
* radius the radius to set.
public void setCornerRadius(float radius) {
setCornerRadius(radius, radius, radius, radius);
* Set the corner radius of a specific corner in px.
* corner the corner to set.
* radius the corner radius to set in px.
public void setCornerRadius(@Corner int corner, float radius) {
if (mCornerRadii[corner] == radius) {
mCornerRadii[corner] =
updateDrawableAttrs();
updateBackgroundDrawableAttrs(false);
invalidate();
* Set the corner radii of each corner individually. Currently only one unique nonzero value is
* supported.
* topLeft radius of the top left corner in px.
* topRight radius of the top right corner in px.
* bottomRight radius of the bottom right corner in px.
* bottomLeft radius of the bottom left corner in px.
public void setCornerRadius(float topLeft, float topRight, float bottomLeft, float bottomRight) {
if (mCornerRadii[Corner.TOP_LEFT] == topLeft
&& mCornerRadii[Corner.TOP_RIGHT] == topRight
&& mCornerRadii[Corner.BOTTOM_RIGHT] == bottomRight
&& mCornerRadii[Corner.BOTTOM_LEFT] == bottomLeft) {
mCornerRadii[Corner.TOP_LEFT] = topL
mCornerRadii[Corner.TOP_RIGHT] = topR
mCornerRadii[Corner.BOTTOM_LEFT] = bottomL
mCornerRadii[Corner.BOTTOM_RIGHT] = bottomR
updateDrawableAttrs();
updateBackgroundDrawableAttrs(false);
invalidate();
public float getBorderWidth() {
return mBorderW
public void setBorderWidth(@DimenRes int resId) {
setBorderWidth(getResources().getDimension(resId));
public void setBorderWidth(float width) {
if (mBorderWidth == width) { return; }
mBorderWidth =
updateDrawableAttrs();
updateBackgroundDrawableAttrs(false);
invalidate();
public int getBorderColor() {
return mBorderColor.getDefaultColor();
public void setBorderColor(@ColorInt int color) {
setBorderColor(ColorStateList.valueOf(color));
public ColorStateList getBorderColors() {
return mBorderC
public void setBorderColor(ColorStateList colors) {
if (mBorderColor.equals(colors)) { return; }
mBorderColor =
(colors != null) ? colors : ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
updateDrawableAttrs();
updateBackgroundDrawableAttrs(false);
if (mBorderWidth & 0) {
invalidate();
public boolean isOval() {
return mIsO
public void setOval(boolean oval) {
updateDrawableAttrs();
updateBackgroundDrawableAttrs(false);
invalidate();
public Shader.TileMode getTileModeX() {
return mTileModeX;
public void setTileModeX(Shader.TileMode tileModeX) {
if (this.mTileModeX == tileModeX) { return; }
this.mTileModeX = tileModeX;
updateDrawableAttrs();
updateBackgroundDrawableAttrs(false);
invalidate();
public Shader.TileMode getTileModeY() {
return mTileModeY;
public void setTileModeY(Shader.TileMode tileModeY) {
if (this.mTileModeY == tileModeY) { return; }
this.mTileModeY = tileModeY;
updateDrawableAttrs();
updateBackgroundDrawableAttrs(false);
invalidate();
public boolean mutatesBackground() {
return mMutateB
public void mutateBackground(boolean mutate) {
if (mMutateBackground == mutate) { return; }
mMutateBackground =
updateBackgroundDrawableAttrs(true);
invalidate();
xml中使用:
&com.mirrorflower.idolfans.view.CircleImageView
android:id="@+id/head_portrait_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
app:riv_oval="true"
android:src="@drawable/food" /&
OK了!自定义属性还是蛮多的!不过功能很齐全有木有,可以设置圆形,可以单独设置某个角的弧度大小,还可以设置border边界线。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:12841次
排名:千里之外
原创:64篇
(12)(13)(13)(16)(11)

我要回帖

更多关于 闲来棋牌 的文章

 

随机推荐