androidbutton图片
Ⅰ 在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