當前位置:首頁 » 安卓系統 » androidcircle

androidcircle

發布時間: 2022-08-25 17:25:33

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); //繪制圓形

熱點內容
設qq密碼時應該設什麼 發布:2025-01-14 02:13:20 瀏覽:605
劍俠情緣主線腳本 發布:2025-01-14 02:11:05 瀏覽:410
java執行ftp命令 發布:2025-01-14 02:05:21 瀏覽:937
青檸檬編程 發布:2025-01-14 02:05:18 瀏覽:882
下載加密日記本 發布:2025-01-14 02:05:16 瀏覽:538
汽車的假配置有哪些 發布:2025-01-14 02:03:16 瀏覽:41
二次插值演算法 發布:2025-01-14 02:02:01 瀏覽:163
江西頁游伺服器雲主機 發布:2025-01-14 02:01:17 瀏覽:492
安卓配置描述文件在哪裡 發布:2025-01-14 01:51:21 瀏覽:260
android數據xml存儲 發布:2025-01-14 01:48:45 瀏覽:369