當前位置:首頁 » 安卓系統 » androidbutton圖片

androidbutton圖片

發布時間: 2022-04-29 14:50:14

Ⅰ 在android中如何實現以下效果,即在圖片上設置button,並且可以控制button的位置

他這個是在你寫的根layou設置背景為一張圖片,然後添加button控制項就是了,可以用RelativeLayout,然後對button控制項添加屬性android:alignParentBottom="true"他就顯示在底部了,其他的都差不多

Ⅱ android中button上設置圖片

android中button上設置圖片的方法為:
1、自定義MyButton類
public class MyButton extends Button {
//This constructormust be
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyButton(Context context) {
super(context);
}
private Paint mPaint = null;
private String mText;
private int mX, mY;
public void onSetText(String text, int nLeft, int nBottom, int nTextSize,
int nTextColor) {
mPaint = new Paint();
mPaint.setTextSize(nTextSize);
mPaint.setColor(nTextColor);
this.mText = text;
this.mX = nLeft;
this.mY = nBottom;
}
private int mDownBmpId, mUpBmpId;
public void onSetBmp(int nDownID, int nUpID) {
this.mDownBmpId = nDownID;
this.mUpBmpId = nUpID;
}
@Override
public void onDraw(Canvas canvas) {
if (mPaint != null)
canvas.drawText(mText, mX, mY, mPaint);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
super.setBackgroundResource(mDownBmpId);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
super.setBackgroundResource(mUpBmpId);
}
return super.onTouchEvent(event);
}
}

2、 在xml布局文件中添加MyButton控制項,像應用普通的Button控制項一樣。
<com.MyButton
android:id="@+id/test_btn" android:layout_width="120px"
android:layout_height="fill_parent" android:text="Test"
android:background="@drawable/btn_u" />
其中com.MyButton是你定義的MyButton類所在的包名
3、在onCreate()中載入MyButton控制項。
MyButton btn = (MyButton)findViewById(R.id.test_btn);
btn.onSetBmp(R.drawable.btn_d, R.drawable.btn_u);
其中btn_d表示為按下btn時背景圖片,btn_u為默認狀態下btn背景圖片。

Ⅲ Android 怎麼讓圖片顯示在button中間

使用ImageButton就可以實現,代碼如下:
下面是scaleType的幾種屬性的含義:
ImageView.ScaleType.CENTER|android:scaleType="center"
按圖片的原來size居中顯示,當圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示
ImageView.ScaleType.CENTER_CROP|android:scaleType="centerCrop"
按比例擴大圖片的size居中顯示,使得圖片長
(寬)等於或大於View的長(寬)
ImageView.ScaleType.CENTER_INSIDE|android:scaleType="centerInside"
將圖片的內容完整居中顯示,通過按比例縮小或原來的size使得圖片長(寬)等於或小於View的長(寬)
ImageView.ScaleType.FIT_CENTER|android:scaleType="fitCenter"
把圖片按比例擴大(縮小)到View的寬度,居中顯示
ImageView.ScaleType.FIT_END|android:scaleType="fitEnd"
把圖片按比例擴大(縮小)到View的寬度,顯示在View的下部分位置
ImageView.ScaleType.FIT_START|android:scaleType="fitStart"
把圖片按比例擴大(縮小)到View的寬度,顯示在View的上部分位置
ImageView.ScaleType.FIT_XY|android:scaleType="fitXY"
把圖片按照指定的大小在View中顯示
ImageView.ScaleType.MATRIX|android:scaleType="matrix"
用matrix來繪制

Ⅳ android,Button設置圖片,寬和高都設置wrap_content,但是設置的圖片還是有背景溢出

圖片設在背景里,android:background="@drawable/ys",這樣就沒問題了。

Ⅳ 我想在android 平台上實現多個button,當點擊到某個button時,button顯示一種圖片,並將焦點設置在上面。

RadioGroup里套RadioButton.

<RadioGroup android:orientation="horizontal" android:id="@+id/main_group" android:layout_width="fill_parent" android:layout_height="wrap_content">
<RadioButton android:id="@+id/main_group_radio1"
android:layout_weight="10" android:layout_height="wrap_content"
android:button="@drawable/main_group_radio1s" />
<RadioButton android:id="@+id/main_group_radio2"
android:layout_weight="10" android:layout_height="wrap_content"
android:button="@drawable/main_group_radio2s" />
</RadioGroup>

android:button="@drawable/main_group_radio2s" 這個是一個配置文件,裡面設定按鈕選中和沒選中的狀態,放到圖片那個文件夾里.
文件名設置成main_group_radio2s就行了
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@drawable/sam" />沒選中的圖片
<item android:state_checked="true"
android:drawable="@drawable/over_01" />選中的圖片
</selector>

Ⅵ android中button上的圖案可以自定義嗎

可以的。
package com.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.Button;

public class ImageTextButton2 extends Button {

private int resourceId = 0;
private Bitmap bitmap;

public ImageTextButton2(Context context) {
super(context,null);
}

public ImageTextButton2(Context context,AttributeSet attributeSet) {
super(context, attributeSet);
this.setClickable(true);
resourceId = R.drawable.icon;
bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
}

public void setIcon(int resourceId)
{
this.bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub

// 圖片頂部居中顯示
int x = (this.getMeasuredWidth() - bitmap.getWidth())/2;
int y = 0;
canvas.drawBitmap(bitmap, x, y, null);
// 坐標需要轉換,因為默認情況下Button中的文字居中顯示
// 這里需要讓文字在底部顯示
canvas.translate(0,(this.getMeasuredHeight()/2) - (int) this.getTextSize());
super.onDraw(canvas);
}

}

Ⅶ 在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裡面button的背景圖片大小怎麼能根據button大小縮放

<p>可以在ps中編輯到合適的解析度
</p><p>可以把Button組件和一個ImageView組件放在一個FrameLayout布局中,規定好FrameLayout的尺寸,然後對上面的兩個組件的寬和高都設置為match_parent</p><p>可以把上述兩個組件換成一個ImageButton組件</p><p>如果需要圖片的切換,可以考慮ImageSwitcher組件</p><p>圖片縮放演算法,縮放好後,動態為ImageView添加圖片
</p><p>
</p>

Ⅸ Android中如何判斷兩個Button的背景圖片是否相等

android 根據button不同狀態顯示不同(背景)圖片
2011-02-24 | nedvedno1 | 轉藏(19)
網上的思路不錯,我只做了基於xml的實現。先來貼一段網上的經典code:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@drawable/button_add" />
<item android:state_pressed="true" android:drawable="@drawable/button_add_pressed" />
<item android:state_focused="true" android:drawable="@drawable/button_add_pressed" />
<item android:drawable="@drawable/button_add" />
</selector>
這個文件放在drawable目錄下面。命名為button_add_x.xml
使用的時候
<ImageButton
android:id="@+id/ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:src="@drawable/button_add_x"
>
</ImageButton>
按照這種方法 我試了多次,發現根本不能生效。後來才發現,這方法是針對ImageButton的,於是查了一下Button和ImageButton的區別,如果區別不大,我就打算把我自己的Button換成ImageButton了,但結果令我失望了,區別還不小呢,主要是這2種控制項對於實現onClick方法的機制不同,想想就復雜,如果貿然換了,編譯的時候恐怕要出現很多error。
再著,這方法並沒有改變button的background,而我們基於button去實現圖片一般都是直接+background的,對么?
下面貼我的code:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@drawable/xxx1" />
<item android:state_pressed="true" android:drawable="@drawable/xxx2" />
<item android:state_focused="true" android:drawable="@drawable/xxx3" />
<-- 這里還可以加N多效果和動作 只要你用的到 -->
<item android:drawable="@drawable/xxx4" />
</selector>這個文件沒有不同,起名為button_add_x.xml 放在drawable下。
使用的時候有所不同
<Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_add_x" <---這里background直接指向剛剛編寫的文件
//這行不需要--->android:src="@drawable/button_add_x"
>
</Button>ok,編譯 push後就生效了。如果你原來的background指向的是一張.png圖片的話,那麼把它換成一個自己製作的文件就好。

Ⅹ android 編程怎樣在圖片里添加Button按鈕。

外面用布局
設置布局的背景為圖片
然後布局裡面添加一個button

熱點內容
我的世界正版好玩的pvp伺服器 發布:2024-04-19 16:28:38 瀏覽:17
光遇安卓渠道服怎麼更換實名認證 發布:2024-04-19 16:18:08 瀏覽:263
關閉ip訪問 發布:2024-04-19 15:59:18 瀏覽:730
大屏安卓系統哪個好 發布:2024-04-19 15:49:10 瀏覽:792
解壓瀏覽器 發布:2024-04-19 15:39:22 瀏覽:575
pythonutfgbk 發布:2024-04-19 15:32:20 瀏覽:221
騰訊雲伺服器搭建網站打不開 發布:2024-04-19 15:32:15 瀏覽:967
光遇腳本輔助下載免費ios 發布:2024-04-19 15:22:11 瀏覽:243
表格VB腳本 發布:2024-04-19 15:13:07 瀏覽:780
python生成器表達式 發布:2024-04-19 15:12:10 瀏覽:747