當前位置:首頁 » 安卓系統 » android圓形控制項

android圓形控制項

發布時間: 2022-09-08 03:48:49

A. 可以用Android studio 設計出如下損失的控制上下左右及各方位移動的圓形控制項嗎

這是自定義控制項, 跟android studio沒有任何關系, 安卓虛擬搖桿, 我這有現成的代碼, 類似游戲中的360度拖動

B. 安卓imageview控制項怎麼設置成圓形

首先,定義定義圓形Imageview類:

java">
importandroid.content.Context;

importandroid.graphics.Bitmap;
importandroid.graphics.Bitmap.Config;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.PorterDuff.Mode;
importandroid.graphics.PorterDuffXfermode;
importandroid.graphics.Rect;
importandroid.graphics.drawable.BitmapDrawable;
importandroid.graphics.drawable.Drawable;
importandroid.util.AttributeSet;
importandroid.widget.ImageView;

{

publicRoundImageView(Contextcontext){
super(context);
//TODOAuto-generatedconstructorstub
}

publicRoundImageView(Contextcontext,AttributeSetattrs){
super(context,attrs);
}

publicRoundImageView(Contextcontext,AttributeSetattrs,intdefStyle){
super(context,attrs,defStyle);
}

@Override
protectedvoidonDraw(Canvascanvas){

Drawabledrawable=getDrawable();

if(drawable==null){
return;
}

if(getWidth()==0||getHeight()==0){
return;
}

Bitmapb=((BitmapDrawable)drawable).getBitmap();

if(null==b)
{
return;
}

Bitmapbitmap=b.(Bitmap.Config.ARGB_8888,true);

intw=getWidth(),h=getHeight();


BitmaproundBitmap=getCroppedBitmap(bitmap,w);
canvas.drawBitmap(roundBitmap,0,0,null);

}

(Bitmapbmp,intradius){
Bitmapsbmp;
if(bmp.getWidth()!=radius||bmp.getHeight()!=radius)
sbmp=Bitmap.createScaledBitmap(bmp,radius,radius,false);
else
sbmp=bmp;
Bitmapoutput=Bitmap.createBitmap(sbmp.getWidth(),
sbmp.getHeight(),Config.ARGB_8888);
Canvascanvas=newCanvas(output);

finalintcolor=0xffa19774;
finalPaintpaint=newPaint();
finalRectrect=newRect(0,0,sbmp.getWidth(),sbmp.getHeight());

paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0,0,0,0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(sbmp.getWidth()/2+0.7f,sbmp.getHeight()/2+0.7f,
sbmp.getWidth()/2+0.1f,paint);
paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp,rect,rect,paint);


returnoutput;
}

}

然後在別的布局文件中使用該控制項即可,如:

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/side_right"
android:gravity="center"
android:orientation="vertical">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="35dip"
android:orientation="vertical">

<<spanstyle="color:#ff0000;">com.founder.reader.view.RoundImageView</span>
android:id="@+id/right_login_head"
android:layout_width="60dip"
android:layout_height="60dip"
android:scaleType="centerInside"
android:src="@drawable/user"/>

C. android怎樣在代碼中創建shape圓oval

  1. 在drawable文件夾中創建bg_oval_shape.xml的xml文件

  2. 文件中添加如下代碼

<?xmlversion="1.0"encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#676767"/>

</shape>

3.在需要添加oval的控制項中引用,代碼如下:

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bg_oval_shape"/>

D. android 開發 imgview 怎麼弄成圓形

imageview的屬性中可以加入background來定義它的背景,將背景定義成一個圓形的drawable就可以了。
另一種辦法,也可以自己定義一個view來顯示圓形的圖片,你可以參考http://blog.csdn.net/alan_biao/article/details/17379925來進行設計。

E. android 怎麼把button變成圓形

使用shape,請看下面截圖,例子來自於android學習手冊,360手機助手中下載,裡面有108個例子、源碼還有文檔。



<?xml version="1.0" encoding="utf-8"?>

<shape

xmlns:Android="http://schemas.android.com/apk/res/android"

android:shape="oval">

<!-- 填充的顏色 -->

<solid android:color="#FFFFFF"/>

<!-- 設置按鈕的四個角為弧形 -->

<!-- android:radius 弧形的半徑 -->

<corners android:radius="360dip"/>

<!-- padding: Button 裡面的文字與Button邊界的間隔 -->

<padding

android:left="10dp"

android:top="10dp"

android:right="10dp"

android:bottom="10dp"

/>

</shape>

-----Main layout文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/soft_info"

/>

<!—直接設置背景 -->

<Button

android:id="@+id/roundBtn1"

android:background="@drawable/btn_oval"

android:layout_width="50dip"

android:layout_height="50dip"

/>

<!— 調用shape自定義xml文件 -->

<Button

android:id="@+id/roundBtn"

android:text="橢圓按鈕"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/main_menu_btnshape"

/>

</LinearLayout>

----acitivity文件

public class MyLifeActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

}

F. android中,如何做圓形的button按鈕

自己繪制圓形的圖片,然後在button布局裡面用BackgroundDrawable設置為button背景。android中是不帶圓形的button的

G. android swiperefreshlayout 為什麼是圓形的

其實主要靠:paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));這行代碼,為什麼呢,我給大家解釋下,SRC_IN這種模式,兩個繪制的效果疊加後取交集展現後圖,怎麼說呢,咱們第一個繪制的是個圓形,第二個繪制的是個Bitmap,於是交集為圓形,展現的是BItmap,就實現了圓形圖片效果。圓角,其實就是先繪制圓角矩形,是不是很簡單,以後別人再說實現圓角,你就把這一行代碼給他就行了。

從Android的示例中,給大家證明一下:
下面有一張PorterDuff.Mode的16中效果圖,咱們的只是其一:

源碼咱們只關心誰先誰後繪制的:

[java] view plain
canvas.translate(x, y);
canvas.drawBitmap(mDstB, 0, 0, paint);
paint.setXfermode(sModes[i]);
canvas.drawBitmap(mSrcB, 0, 0, paint);
paint.setXfermode(null);
canvas.restoreToCount(sc);
可以看出先繪制的Dst,再繪制的Src,最後的展示是SrcIn那個圖:顯示的區域是二者交集,展示的是Src(後者)。和咱們前面結論一致。效果16種,大家可以自由組合展示不同的效果。

好了,原理和核心代碼解釋完成。下面開始寫自定義View。
1、自定義屬性:

[html] view plain
<?xml version="1.0" encoding="utf-8"?>
<resources>

<attr name="borderRadius" format="dimension" />
<attr name="type">
<enum name="circle" value="0" />
<enum name="round" value="1" />
</attr>
<attr name="src" format="reference"></attr>

<declare-styleable name="CustomImageView">
<attr name="borderRadius" />
<attr name="type" />
<attr name="src" />
</declare-styleable>

</resources>

2、構造中獲取自定義的屬性:

[java] view plain
/**
* TYPE_CIRCLE / TYPE_ROUND
*/
private int type;
private static final int TYPE_CIRCLE = 0;
private static final int TYPE_ROUND = 1;

/**
* 圖片
*/
private Bitmap mSrc;

/**
* 圓角的大小
*/
private int mRadius;

/**
* 控制項的寬度
*/
private int mWidth;
/**
* 控制項的高度
*/
private int mHeight;

public CustomImageView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}

public CustomImageView(Context context)
{
this(context, null);
}

/**
* 初始化一些自定義的參數
*
* @param context
* @param attrs
* @param defStyle
*/
public CustomImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);

TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyle, 0);

int n = a.getIndexCount();
for (int i = 0; i < n; i++)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.CustomImageView_src:
mSrc = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0));
break;
case R.styleable.CustomImageView_type:
type = a.getInt(attr, 0);// 默認為Circle
break;
case R.styleable.CustomImageView_borderRadius:
mRadius= a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f,
getResources().getDisplayMetrics()));// 默認為10DP
break;
}
}
a.recycle();
}

3、onMeasure中獲取控制項寬高:

[java] view plain
/**
* 計算控制項的高度和寬度
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
/**
* 設置寬度
*/
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);

if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
{
mWidth = specSize;
} else
{
// 由圖片決定的寬
int desireByImg = getPaddingLeft() + getPaddingRight()
+ mSrc.getWidth();
if (specMode == MeasureSpec.AT_MOST)// wrap_content
{
mWidth = Math.min(desireByImg, specSize);
} else

mWidth = desireByImg;
}

/***
* 設置高度
*/

specMode = MeasureSpec.getMode(heightMeasureSpec);
specSize = MeasureSpec.getSize(heightMeasureSpec);
if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
{
mHeight = specSize;
} else
{
int desire = getPaddingTop() + getPaddingBottom()
+ mSrc.getHeight();

if (specMode == MeasureSpec.AT_MOST)// wrap_content
{
mHeight = Math.min(desire, specSize);
} else
mHeight = desire;
}

setMeasuredDimension(mWidth, mHeight);

}

4、根據Type繪制:

[java] view plain
/**
* 繪制
*/
@Override
protected void onDraw(Canvas canvas)
{

switch (type)
{
// 如果是TYPE_CIRCLE繪制圓形
case TYPE_CIRCLE:

int min = Math.min(mWidth, mHeight);
/**
* 長度如果不一致,按小的值進行壓縮
*/
mSrc = Bitmap.createScaledBitmap(mSrc, min, min, false);

canvas.drawBitmap(createCircleImage(mSrc, min), 0, 0, null);
break;
case TYPE_ROUND:
canvas.drawBitmap(createRoundConerImage(mSrc), 0, 0, null);
break;

}

}

/**
* 根據原圖和變長繪制圓形圖片
*
* @param source
* @param min
* @return
*/
private Bitmap createCircleImage(Bitmap source, int min)
{
final Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888);
/**
* 產生一個同樣大小的畫布
*/
Canvas canvas = new Canvas(target);
/**
* 首先繪制圓形
*/
canvas.drawCircle(min / 2, min / 2, min / 2, paint);
/**
* 使用SRC_IN,參考上面的說明
*/
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
/**
* 繪制圖片
*/
canvas.drawBitmap(source, 0, 0, paint);
return target;
}

/**
* 根據原圖添加圓角
*
* @param source
* @return
*/
private Bitmap createRoundConerImage(Bitmap source)
{
final Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(target);
RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rect, mRadius, mRadius, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(source, 0, 0, paint);
return target;
}

好了,我就不解析代碼了,自定義View這是第五篇了,,,,寫得好惡心,,,,

H. android 如何把正方形圖片顯示圓形

Android應用開發中,很多頭像都要求顯示成圓形的,這就可以使用android的canvas、paint這些類來進行設置圓形,先設置paint的樣式為圓形,然後把你要設置成圓形的圖片重新賦值給paint這個類:canvas.drawBitmap(tempBmp, rect, rect, paint);

核心代碼如下(引用這位前輩:http://blog.sina.com.cn/s/blog_7607703f0101dhlj.html,我增加一些注釋,原來是沒有注釋):

packagecom.liang.round;
importandroid.annotation.SuppressLint;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.Bitmap.Config;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Canvas;
importandroid.graphics.Paint;
importandroid.graphics.PorterDuff;
importandroid.graphics.PorterDuffXfermode;
importandroid.graphics.Rect;
importandroid.view.View;
publicclassMyViewextendsView{
privateBitmapbmp=null;
privatePaintpaint=null;
publicMyView(Contextcontext){
super(context);
//TODOAuto-generatedconstructorstub
paint=newPaint();//實例化畫筆類
BitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeResource(context.getResources(),R.drawable.test,options);//獲得你存放在drawable下的正方形圖片
options.inJustDecodeBounds=false;
BitmaptempBmp=BitmapFactory.decodeResource(context.getResources(),R.drawable.test,options);//實例化一個bitmap圖片類
intwidth=options.outWidth;
intheight=options.outHeight;
intsize=width>height?height:width;//邊框
intpos=(int)(size/2);
doubleradius=pos*Math.sin(45*180/Math.PI);//半徑
size=(int)(radius*2);
pos=(int)(size/2);
bmp=Bitmap.createBitmap(size,size,Config.ARGB_8888);
Canvascanvas=newCanvas(bmp);
Rectrect=newRect(0,0,size,size);
paint.setAntiAlias(true);
canvas.drawCircle(pos,pos,(float)radius,paint);
paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
canvas.drawBitmap(tempBmp,rect,rect,paint);
tempBmp.recycle();
}
@SuppressLint("DrawAllocation")
@Override
protectedvoidonDraw(Canvascanvas){
//TODOAuto-generatedmethodstub
super.onDraw(canvas);
if(bmp!=null){
if(!bmp.isRecycled()){
canvas.drawBitmap(bmp,100,100,paint);
}
}
}
}

I. 如何實現Android的圓形懸浮球

Android 的一種控制項:FloatActionButton,直接使用

J. android 自定義載入圓環控制項裡面怎麼設置三行字

public class MyView extends View{
//此處省略構造方法

private void onDraw(Canvas canvas){
//重寫view的onDraw方法,繪制控制項的樣式
//這里你使用canvas來繪制,你布局中使用這個控制項就是你繪制的樣子
}

//然後你可以定義很多自己的一些方法,用來修改控制項的樣式
//假如你自定義的一個進度條的話,就要修改進度條值,你就可以自定義方法,讓實現對象來改變進度值,記得修改後調用validate方法更新顯示。(具體函數記不太清了)
}

大概就是這樣實現的自定義控制項,自定義控制項的話優化是很重要的哦,不然性能會很差。
然後你要使用這個控制項的話,在布局中就需要這樣定義,假如這個自定義控制項類是這樣的:
xxx.xxx.MyView。
則使用時:
<xxx.xxx.MyView
這些地方一樣的設置寬高,id啊雜七雜八的屬性

/>

熱點內容
門口機sip伺服器ip是什麼 發布:2024-05-17 17:38:27 瀏覽:553
光遇安卓區是什麼服 發布:2024-05-17 17:22:25 瀏覽:24
linux驅動開發教程 發布:2024-05-17 17:19:52 瀏覽:501
抖音中秋節視頻腳本 發布:2024-05-17 17:19:51 瀏覽:194
快遞櫃為什麼用安卓系統 發布:2024-05-17 17:17:18 瀏覽:907
電腦配置光纖介面怎麼標注 發布:2024-05-17 17:06:56 瀏覽:977
如何用方向鍵控制安卓機 發布:2024-05-17 16:38:11 瀏覽:199
雨田系統源碼 發布:2024-05-17 16:28:06 瀏覽:587
新手直播腳本 發布:2024-05-17 16:27:25 瀏覽:848
python雙引號單引號 發布:2024-05-17 16:19:31 瀏覽:949