public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view= View.inflate(this,R.layout.activity_main,null);
//布局参数
ViewGroup.LayoutParams layoutParams=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
ImageView imageView=new ImageView(this);
//设置布局参数
imageView.setLayoutParams(layoutParams);
imageView.setBackgroundColor(Color.RED);
//把textview加入RelativeLayout中
((RelativeLayout)view).addView(imageView);
//显示整个布局
setContentView(view);
setContentView(new SmileyView(this));
}
}
class SmileyView extends View {
private Paint mCirclePaint;
private float mCenterX;
private float mCenterY;
private float mRadius;
public SmileyView(Context context) {
this(context, null);
}
public SmileyView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SmileyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaints();
}
private void initPaints() {
mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setStyle(Paint.Style.FILL);
mCirclePaint.setColor(Color.YELLOW);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = MeasureSpec.getSize(heightMeasureSpec);
int size = Math.min(w, h);
setMeasuredDimension(size, size);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mCenterX = w / 2f;
mCenterY = h / 2f;
mRadius = Math.min(w, h) / 2f;
}
protected void onDraw(Canvas canvas) {
canvas.drawCircle(mCenterX, mCenterY, mRadius, mCirclePaint);
// draw eyes
}
}
上面这个代码如何修改为在ImageView上显示画的圆,然后把ImageView转化成Bitmap?麻烦具体点,我小白