package com.example.administrator.woyunying.activity;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import com.example.administrator.woyunying.R;
/**
* Created by 陈猛 on 2016/2/24.
*/
public class MyTest extends Activity {
private static final int CIRCLE_RADIUS_DP = 50;//circle_radile_dp
static int sScreenWidth;
static int sProfileImageHeight;
View view;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
sScreenWidth = getResources().getDisplayMetrics().widthPixels;//屏幕宽度
sProfileImageHeight = getResources().getDimensionPixelSize(R.dimen.height_profile_image);//介绍图片的高度 将dp转换为sp
view=findViewById(R.id.view);
view.setBackground(buildAvatarCircleOverlay());
}
private ShapeDrawable buildAvatarCircleOverlay() {
int radius = 660;//
float[] outerR = new float[] { 80, 80, 80, 80, 80, 80, 80, 80 };
ShapeDrawable overlay = new ShapeDrawable(new RoundRectShape(outerR ,//外部矩形的四个角的弧度
new RectF(//内外边的距离,也可以理解为左上角和右下角的坐标(坐标的原点不是屏幕的左上角)
sScreenWidth / 2 - dpToPx(getCircleRadiusDp() * 2),
sProfileImageHeight / 2 - dpToPx(getCircleRadiusDp() * 2),
sScreenWidth / 2 - dpToPx(getCircleRadiusDp() * 2),
sProfileImageHeight / 2 - dpToPx(getCircleRadiusDp() * 2)),
new float[]{radius, radius, radius, radius, radius, radius, radius, radius}));
overlay.getPaint().setColor(getResources().getColor(R.color.red));//设置画笔的颜色
overlay.getPaint().setStyle(Paint.Style.FILL);
return overlay;
}
public int dpToPx(int dp) {
return Math.round((float) dp * getResources().getDisplayMetrics().density);
}
protected int getCircleRadiusDp() {
return CIRCLE_RADIUS_DP;
}
}
效果图