我有一个textview.i需要像上面那样设置textview的背景 通过考虑textview的高度和宽度使用drawable的图像。我可以创建一个像上面这样的背景 在画布上画画。我还需要改变内在的颜色 动态圈。
到目前为止我的代码
public class BackGround extends Drawable {
private Paint mPaint;
private Paint mPaint1;
@Override
public void draw(Canvas canvas) {
// Set the correct values in the Paint
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mPaint.setAntiAlias(true);
mPaint1 = new Paint();
mPaint1.setColor(Color.WHITE);
mPaint1.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint1.setFlags(Paint.ANTI_ALIAS_FLAG);
mPaint1.setAntiAlias(true);
canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, canvas.getWidth() - (canvas.getWidth() / 4), mPaint);
canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() - (canvas.getHeight() / 6), 5, mPaint1);
}
@Override
public int getOpacity() {
return PixelFormat.OPAQUE;
}
@Override
public void setAlpha(int arg0) {
}
@Override
public void setColorFilter(ColorFilter arg0) {
}
}
答案 0 :(得分:1)
EDIT @ 2
使用xml以这种方式执行
<item>
<shape android:shape="oval" >
<solid android:color="#000000" />
<size
android:height="15dp"
android:width="15dp" />
<padding
android:bottom="5dp"
android:left="20dp"
android:right="20dp"
android:top="40dp" />
</shape>
</item>
<item>
<shape android:shape="oval" >
<stroke
android:width="5dp"
android:color="@android:color/transparent" />
<solid android:color="#f00" />
<size
android:height="10dp"
android:width="10dp" />
</shape>
</item>
答案 1 :(得分:0)
EDIT @ 1
尝试这种方式在画布中画圆圈
public class MainActivity extends Activity {
Draw draw;
LinearLayout linearLayout;
public void onCreate(Bundle s) {
super.onCreate(s);
setContentView(R.layout.test1);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
draw = new Draw(this);
linearLayout.addView(draw);
}
public class Draw extends View {
Paint paint = new Paint();
Draw(Context context) {
super(context);
}
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLUE);
canvas.drawCircle(120, 120, 40, paint);
paint.setColor(Color.RED);
canvas.drawCircle(120, 140, 10, paint);
}
}
}