android圓形背景
❶ 在android中怎樣讓按鈕漂浮在圖片上
android懸浮按鈕(Floating action button)的兩種實現方法
最近android中有很多新的設計規范被引入,最流行的莫過於被稱作Promoted Actions的設計了,Promoted Actions是指一種操作按鈕,它不是放在actionbar中,而是直接在可見的UI布局中(當然這里的UI指的是setContentView所管轄的范圍)。因此它更容易在代碼中被獲取到(試想如果你要在actionbar中獲取一個菜單按鈕是不是很難?),Promoted Actions往往主要用於一個界面的主要操作,比如在email的郵件列表界面,promoted action可以用於接受一個新郵件。promoted action在外觀上其實就是一個懸浮按鈕,更常見的是漂浮在界面上的圓形按鈕,一般我直接將promoted action稱作懸浮按鈕,英文名稱Float Action Button簡稱(FAB,不是FBI哈)。
floatactionbutton是android l中的產物,但是我們也可以在更早的版本中實現。假設我這里有一個列表界面,我想使用floatactionbutton代表添加新元素的功能,界面如下:
要實現floatactionbutton可以有多種方法,一種只適合android L,另外一種適合任意版本。
用ImageButton實現
這種方式其實是在ImageButton的屬性中使用了android L才有的一些特性:
<ImageButton
android:layout_width="56dp"
android:layout_height="56dp"
android:src="@drawable/plus"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:tint="@android:color/white"
android:id="@+id/fab"
android:elevation="1dp"
android:background="@drawable/ripple"
android:stateListAnimator="@anim/fab_anim"
/>
仔細一點,你會發現我們將這個ImageButton放到了布局的右下角,為了實現floatactionbutton應該具備的效果,需要考慮以下幾個方面:
·Background
·Shadow
·Animation
背景上我們使用ripple drawable來增強吸引力。注意上面的xml代碼中我們將background設置成了@drawable/ripple,ripple drawable的定義如下:
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
<item>
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
既然是懸浮按鈕,那就需要強調維度上面的感覺,當按鈕被按下的時候,按鈕的陰影需要擴大,並且這個過程是漸變的,我們使用屬性動畫去改變translatioz。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<objectAnimator
android:ration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="@dimen/start_z"
android:valueTo="@dimen/end_z"
android:valueType="floatType" />
</item>
<item>
<objectAnimator
android:ration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="@dimen/end_z"
android:valueTo="@dimen/start_z"
android:valueType="floatType" />
</item>
</selector>
使用自定義控制項的方式實現懸浮按鈕
這種方式不依賴於android L,而是碼代碼。
首先定義一個這樣的類:
public class CustomFAB extends ImageButton {
...
}
然後是讀取一些自定義的屬性(假設你了解styleable的用法)
private void init(AttributeSet attrSet) {
Resources.Theme theme = ctx.getTheme();
TypedArray arr = theme.obtainStyledAttributes(attrSet, R.styleable.FAB, 0, 0);
try {
setBgColor(arr.getColor(R.styleable.FAB_bg_color, Color.BLUE));
setBgColorPressed(arr.getColor(R.styleable.FAB_bg_color_pressed, Color.GRAY));
StateListDrawable sld = new StateListDrawable();
sld.addState(new int[] {android.R.attr.state_pressed}, createButton(bgColorPressed));
sld.addState(new int[] {}, createButton(bgColor));
setBackground(sld);
}
catch(Throwable t) {}
finally {
arr.recycle();
}
}
在xml中我們需要加入如下代碼,一般是在attr.xml文件中。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FAB">
<!-- Background color -->
<attr name="bg_color" format="color|reference"/>
<attr name="bg_color_pressed" format="color|reference"/>
</declare-styleable>
</resources>
使用StateListDrawable來實現不同狀態下的背景
private Drawable createButton(int color) {
OvalShape oShape = new OvalShape();
ShapeDrawable sd = new ShapeDrawable(oShape);
setWillNotDraw(false);
sd.getPaint().setColor(color);
OvalShape oShape1 = new OvalShape();
ShapeDrawable sd1 = new ShapeDrawable(oShape);
sd1.setShaderFactory(new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
LinearGradient lg = new LinearGradient(0,0,0, height,
new int[] {
Color.WHITE,
Color.GRAY,
Color.DKGRAY,
Color.BLACK
}, null, Shader.TileMode.REPEAT);
return lg;
}
});
LayerDrawable ld = new LayerDrawable(new Drawable[] { sd1, sd });
ld.setLayerInset(0, 5, 5, 0, 0);
ld.setLayerInset(1, 0, 0, 5, 5);
return ld;
}
最後將控制項放xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.survivingwithandroid.fab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
...
<com.survivingwithandroid.fab.CustomFAB
android:layout_width="56dp"
android:layout_height="56dp"
android:src="@android:drawable/ic_input_add"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
custom:bg_color="@color/light_blue"
android:tint="@android:color/white"
/>
</RelativeLayout>
❷ android progressbar的白色背景怎麼去掉
下面這個圖片是progressbar的各種樣式,不知道能不能滿足您的要求,例子來自於android學習手冊,360手機助手中可以下載,裡面有108個android例子,點擊源碼可以看源碼,點擊文檔可以看文檔。
1、說明
在某些操作的進度中的可視指示器,為用戶呈現操作的進度,還它有一個次要的進度條,用來顯示中間進度,如在流媒體播放的緩沖區的進度。一個進度條也可不確定其進度。在不確定模式下,進度條顯示循環動畫。這種模式常用於應用程序使用任務的長度是未知的。
2、XML重要屬性
android:progressBarStyle:默認進度條樣式
android:progressBarStyleHorizontal:水平樣式
3 重要方法
getMax():返回這個進度條的范圍的上限
getProgress():返回進度
getSecondaryProgress():返回次要進度
incrementProgressBy(int diff):指定增加的進度
isIndeterminate():指示進度條是否在不確定模式下
setIndeterminate(boolean indeterminate):設置不確定模式下
setVisibility(int v):設置該進度條是否可視
4 重要事件
onSizeChanged(int w, int h, int oldw, int oldh):當進度值改變時引發此事件
5進度條的樣式
Widget.ProgressBar.Horizontal長形進度
Androidxml 布局:
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal "
/>
源碼:
private ProgressBar mProgress;
private int mProgressStatus=0;
private Handler mHandler=newHandler();
@Override
protected void onCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgress=(ProgressBar)findViewById(R.id.progress_bar);
new Thread(new Runnable(){
@Override
public void run(){
while(mProgressStatus<100){
mProgressStatus=doWork();
mHandler.post(new Runnable(){
@Override
public void run(){
mProgress.setProgress(mProgressStatus);
}
});
}
}
}).start();
}
效果圖:
帶第二進度的進度條
xml配置如下:
<ProgressBar
android:id="@+id/progress_bar_with_second"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="40"
android:secondaryProgress="70"
android:paddingTop="20dp"
android:paddingBottom="20dp"/>
這里我們設置了初始的進度為40,android:progress的值在mini和max之間即mini<=progressvalue<=max
設置了第二進度條的進度值為70,該值也在mini和max之間。
效果如下:
不確定模式進度條
xml配置文件:
<ProgressBar
android:id="@+id/progress_bar_indeterminate"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateBehavior="cycle"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:progress="40" />
這里通過android:indeterminate="true"設置了當前為無模式進度條
效果如圖:
普通圓形進度:Widget.ProgressBar.Inverse
<ProgressBar
android:id="@+id/progress_bar1"
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="50"
android:background="#ff00ff"
android:paddingTop="4dp" />
通過android:backgroup設置了背景色
❸ 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);
}
}
❹ android中怎麼繪制這種圓形布局
圓形是個背景,可以通過xml定義背景圖片
在res/drawable/下添加背景xml,test.xml代碼如下
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="oval">
<padding android:top="2dp" android:right="2dp" android:bottom="2dp" android:left="2dp" />
<solid android:color="#00a0eb"/>
</shape>
</item>
<item >
<shape android:shape="oval">
<solid android:color="#ffffffff"/>
</shape>
</item>
</layer-list>
然後在layout下添加布局文件
代碼如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="indi.zcm.dropdown.MainActivity" >
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/test">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:text="購買人數" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:textSize="26sp"
android:text="32514" />
</RelativeLayout>
</RelativeLayout>
這個應該就是你要的效果
❺ android圓形灰色圖片做按鈕背景,圓形背景上需要疊加一張圖片和文字,文字要在圖片的下方,怎麼實現
用android:drawableTop設置圖片就好了。
❻ android 開發 imgview 怎麼弄成圓形
imageview的屬性中可以加入background來定義它的背景,將背景定義成一個圓形的drawable就可以了。
另一種辦法,也可以自己定義一個view來顯示圓形的圖片,你可以參考http://blog.csdn.net/alan_biao/article/details/17379925來進行設計。