androidcircle
A. android 如何重写imageview 让图片有圆角效果
android 自定义圆角ImageView以及锯齿的处理
看到很多人开发过程中要使用圆角图片时,解决方法有:
1.重新绘制一张图片
2.通过布局来配置
3.通过重写View来实现
其中1,2在这里就不讲了,重点讲讲方法三的实现。
实现一:通过截取画布一个圆形区域与图片的相交部分进行绘制,缺点:锯齿明显,设置Paint,Canvas抗锯齿无效。
package com.open.circleimageview.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.View;
public class CircleImageViewA extends View {
public CircleImageViewA(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CircleImageViewA(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircleImageViewA(Context context) {
super(context);
}
private Bitmap bitmap;
private Rect bitmapRect=new Rect();
private PaintFlagsDrawFilter pdf=new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
private Paint paint = new Paint();
{
paint.setStyle(Paint.Style.STROKE);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);// 设置画笔的锯齿效果。 true是去除,大家一看效果就明白了
}
private Path mPath=new Path();
public void setImageBitmap(Bitmap bitmap)
{
this.bitmap=bitmap;
}
@Override
protected void onDraw(Canvas canvas) {
if(null==bitmap)
{
return;
}
bitmapRect.set(0, 0, getWidth(), getHeight());
canvas.save();
canvas.setDrawFilter(pdf);
mPath.reset();
canvas.clipPath(mPath); // makes the clip empty
mPath.addCircle(getWidth()/2, getWidth()/2, getHeight()/2, Path.Direction.CCW);
canvas.clipPath(mPath, Region.Op.REPLACE);
canvas.drawBitmap(bitmap, null, bitmapRect, paint);
canvas.restore();
}
}
实现二:通过PorterDuffXfermode 方式(注意要设置硬件加速,否则部分机子无效),优点:锯齿基本没有
package com.open.circleimageview.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class CircleImageViewB extends View {
public CircleImageViewB(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CircleImageViewB(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CircleImageViewB(Context context) {
super(context);
init();
}
private Bitmap bitmap;
private Rect bitmapRect=new Rect();
private PaintFlagsDrawFilter pdf=new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
private Paint paint = new Paint();
{
paint.setStyle(Paint.Style.STROKE);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);// 设置画笔的锯齿效果。 true是去除,大家一看效果就明白了
}
private Bitmap mDstB=null;
private PorterDuffXfermode xfermode=new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY);
private void init()
{
try {
if(android.os.Build.VERSION.SDK_INT>=11)
{
setLayerType(LAYER_TYPE_SOFTWARE, null);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void setImageBitmap(Bitmap bitmap)
{
this.bitmap=bitmap;
}
private Bitmap makeDst(int w, int h)
{
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bm);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(Color.parseColor("#ffffffff"));
c.drawOval(new RectF(0, 0, w, h), p);
return bm;
}
@Override
protected void onDraw(Canvas canvas) {
if(null==bitmap)
{
return;
}
if(null==mDstB)
{
mDstB=makeDst(getWidth(), getHeight());
}
bitmapRect.set(0, 0, getWidth(), getHeight());
canvas.save();
canvas.setDrawFilter(pdf);
canvas.drawBitmap(mDstB, 0, 0, paint);
paint.setXfermode(xfermode);
canvas.drawBitmap(bitmap, null, bitmapRect, paint);
paint.setXfermode(null);
canvas.restore();
}
}
B. 小白请求android编程大佬帮忙,怎么样才能慢慢一个接一个显示10个圆
public class DrawCircle extends View
{
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
int color;
int x=0;
int y=0;
int r=0;
int n=0;
int needUpdate = 0;
DrawCircle circle = null;
public DrawCircle(Context con, AttributeSet attr)
{
super(con,attr);
circle = this;
new Thread(new Runnable()
{
@Override
public void run()
{
while(n < 10)
{
Thread.sleep(500);
needUpdate = 1;
circle.postInvalidate();
n ++;
}
}
}).start();
}
@Override
protected void onDraw(Canvas c)
{
super.onDraw(c);
try
{
if (needUpdate == 1)
{
needUpdate = 0;
gen();
p.setColor(color);
p.setStyle(Paint.Style.FILL);
c.drawCircle(x, y, r, p);
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
public void gen()
{
color = Color.rgb(rand(0,255),rand(0,255),rand(0,255));
x=rand(0,1200);
y=rand(0,1200);
r=rand(50,150);
}
public int rand(int a, int b)
{
return((int)((b-a+1)*Math.random() + a));
}
}
手打的 不知道有没有错。你看一下
C. android image asset明明设置的是circle为什么还是方的
具体操作:
1、在项目的values文件底下创建一新的属性文件,文件名为attrs:New->XML->Values XML File:
2、补充attrs.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CircleImageView">
<attr name="border_width" format="dimension" />
<attr name="border_color" format="color" />
</declare-styleable>
</resources>
D. android 怎么在屏幕中间画一个圆
主要运用SurfaceView来实现在屏幕上画一个圆,你可以通过按方向键和触摸屏幕来改变圆的位置 代码: Activity package com.view; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 隐藏状态栏 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 把Activity的标题去掉 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置布局 this.setContentView(new MySurfaceView(this)); } } SurfaceView package com.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; public class MySurfaceView extends SurfaceView implements Runnable, Callback { private SurfaceHolder mHolder; // 用于控制SurfaceView private Thread t; // 声明一条线程 private boolean flag; // 线程运行的标识,用于控制线程 private Canvas mCanvas; // 声明一张画布 private Paint p; // 声明一支画笔 private int x = 50, y = 50, r = 10; // 圆的坐标和半径 public MySurfaceView(Context context) { super(context); mHolder = getHolder(); // 获得SurfaceHolder对象 mHolder.addCallback(this); // 为SurfaceView添加状态监听 p = new Paint(); // 创建一个画笔对象 p.setColor(Color.WHITE); // 设置画笔的颜色为白色 setFocusable(true); // 设置焦点 } /** * 自定义一个方法,在画布上画一个圆 */ public void Draw() { mCanvas = mHolder.lockCanvas(); // 获得画布对象,开始对画布画画 mCanvas.drawRGB(0, 0, 0); // 把画布填充为黑色 mCanvas.drawCircle(x, y, r, p); // 画一个圆 mHolder.unlockCanvasAndPost(mCanvas); // 完成画画,把画布显示在屏幕上 } /** * 当SurfaceView创建的时候,调用此函数 */ @Override public void surfaceCreated(SurfaceHolder holder) { t = new Thread(this); // 创建一个线程对象 flag = true; // 把线程运行的标识设置成true t.start(); // 启动线程 } /** * 当SurfaceView的视图发生改变的时候,调用此函数 */ @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } /** * 当SurfaceView销毁的时候,调用此函数 */ @Override public void surfaceDestroyed(SurfaceHolder holder) { flag = false; // 把线程运行的标识设置成false } /** * 当屏幕被触摸时调用 */ @Override public boolean onTouchEvent(MotionEvent event) { x = (int) event.getX(); // 获得屏幕被触摸时对应的X轴坐标 y = (int) event.getY(); // 获得屏幕被触摸时对应的Y轴坐标 return true; } /** * 当用户按键时调用 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_DPAD_UP){ //当用户点击↑键时 y--; //设置Y轴坐标减1 } return super.onKeyDown(keyCode, event); } @Override public void run() { while (flag) { Draw(); // 调用自定义画画方法 try { Thread.sleep(50); // 让线程休息50毫秒 } catch (InterruptedException e) { e.printStackTrace(); } } } } MySurfaceView首先继承SurfaceView,然后实现Runnable和Callback接口 重写了Runnable的run方法和Callback的surfaceCreated(SurfaceHolder holder),surfaceChanged(SurfaceHolder holder, int format, int width,int height),surfaceDestroyed(SurfaceHolder holder)方法, 还实现了onTouchEvent(MotionEvent event),onKeyDown(int keyCode, KeyEvent event)方法来,详细的在代码里已注释。
E. android canvas.drawcircle 为什么画不出圆
绘制圆的话,drawcircle需要传圆心坐标和半径大小/画笔进去,我猜你缺少对应的参数,可以参考如下代码:
mPaint.setAntiAlias(false); //设置画笔为无锯齿
mPaint.setColor(Color.BLACK); //设置画笔颜色
canvas.drawColor(Color.WHITE); //白色背景
mPaint.setStrokeWidth((float) 3.0); //线宽
mPaint.setStyle(Paint.Style.STROKE); //空心效果
canvas.drawCircle(50, 50, 10, mPaint); //绘制圆形
canvas.drawCircle(100, 100, 20, mPaint); //绘制圆形
canvas.drawCircle(150, 150, 30, mPaint); //绘制圆形
canvas.drawCircle(200, 200, 40, mPaint); //绘制圆形
canvas.drawCircle(250, 250, 50, mPaint); //绘制圆形
canvas.drawCircle(300, 300, 60, mPaint); //绘制圆形
canvas.drawCircle(350, 350, 70, mPaint); //绘制圆形