導航:首頁 > 操作系統 > android按鈕放圖片

android按鈕放圖片

發布時間:2022-12-29 22:42:33

『壹』 android 如何給按鈕加背景圖片,只是單純的添加背景,不用點擊後改變圖片

主要是用到selector這個屬性!

1. android項目中,在res文件夾下建一個drawable文件夾

button_selector.xml
指定好按下時的圖片 和 未按下時的圖片

2. 將button_selector.xml放到drawable文件夾下

main.xml
這樣所指定的ImageButton當點擊的時候就會改變背景圖片了

『貳』 Android 如何通過帽子右下角的按鈕來控制圖片的縮放和旋轉

Android中對圖片處理應用比較常見,所以整理了一些對圖片的基本操作處理功能方法:
/**
* 圖片反轉
* @param img
* @return
*/
public Bitmap toturn(Bitmap img){
Matrix matrix = new Matrix();
matrix.postRotate(90); /*翻轉90度*/
int width = bitmap.getWidth();
int height =bitmap.getHeight();
img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);
return img;
}
/**
* 圖片縮放
* @param bigimage
* @param newWidth
* @param newHeight
* @return
*/
public Bitmap tochange(Bitmap bigimage,int newWidth,int newHeight){
// 獲取這個圖片的寬和高
int width = bigimage.getWidth();
int height = bigimage.getHeight();
// 創建操作圖片用的matrix對象
Matrix matrix = new Matrix();
// 計算縮放率,新尺寸除原始尺寸
float scaleWidth = ((float) newWidth)/width;
float scaleHeight = ((float) newHeight)/height;
// 縮放圖片動作
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.createBitmap(bigimage, 0, 0, width, height,matrix, true);
return bitmap;
}

/**
* 程序切割圖片
* @param bitmap
* @param x
* @param y
* @param w
* @param h
* @return
*/
public Bitmap BitmapClipBitmap(Bitmap bitmap,int x, int y, int w, int h) {
return Bitmap.createBitmap(bitmap, x, y, w, h);
}
/**
* 圖片疊加
* @param b
* @return
*/
public Bitmap diejia(Bitmap b){

if(!b.isMutable()){

『叄』 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中怎樣將一個按鈕放在圖片的右下角

說的詳細點!如果是在圖片為背景,按鈕在圖片的右下角並且和圖片底線對齊:你採用相對布局,在按鈕的布局文件裡面加上這兩句應該就好使了:
android:layout_alignright="@id/iview"
android:layout_alignbottom="@id/iview",iview是圖片控制項的id

『伍』 Android中怎樣用代碼實現將一外部小圖片作為按鈕使用,點擊小圖標會產生頁面轉換

你在添加按鈕的時候直接使用圖片按鈕啊
在res下的drawable裡面添加你要的點擊的圖片
main.xml裡面添加控制項
<ImageButton android:id="@+id/ib"

android:src="@drawable/bookmark_001"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

『陸』 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中怎樣讓按鈕漂浮在圖片上

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平台中設置圖片按鈕也就是該按鈕是個圖片的形式

吧圖片作為按鈕的背景就可以了,android:background="@drawable/xx.jpg"

閱讀全文

與android按鈕放圖片相關的資料

熱點內容
吃雞國際體驗服為什麼伺服器繁忙 瀏覽:92
php中sleep 瀏覽:488
vr怎麼看視頻演算法 瀏覽:84
手機app如何申報個人所得稅零申報 瀏覽:692
如何截獲手機app連接的ip 瀏覽:330
冰箱壓縮機是否需要電容 瀏覽:344
python列表每一行數據求和 瀏覽:274
自己有一台伺服器可以玩什麼 瀏覽:656
社會學波普諾pdf 瀏覽:584
解壓做食物的小視頻 瀏覽:758
pdf怎麼單獨設置文件夾 瀏覽:474
業務邏輯程序員 瀏覽:659
addto新建文件夾什麼意思 瀏覽:160
有伺服器地址怎麼安裝軟體 瀏覽:659
安卓如何完全清除數據 瀏覽:691
安卓安卓證書怎麼信任 瀏覽:54
伺服器被攻擊如何解決 瀏覽:221
學霸變成程序員 瀏覽:883
c語言編譯錯誤fatalerror 瀏覽:443
ipv4內部伺服器地址怎麼分配 瀏覽:464