按钮保持按下状态

时间:2012-07-28 08:10:09

标签: android button

在我的xml文件中,我有这个按钮:

<Button
android:id="@+id/button_8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/Bf"
android:background="@drawable/button_purple" 
android:layout_weight="1"
android:textColor="#ffffff"
android:onClick="action"            
/>

在我的活动中,我有按钮的这种方法:

public void action (View v)
{
    s = "m";
    changeCouleur("blue");
    v.setPressed(true);
}

当我按下按钮时它正在工作,但按钮不会保持按下状态。

我不使用图像这是我用于颜色的图像:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#449def" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item android:state_focused="true" >
        <shape>
            <solid
                android:color="#449def" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#449def"
                android:endColor="#2f6699"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

我尝试切换按钮,但这与我想要做的事情不符。 如果您发现错误,请提前致谢。

1 个答案:

答案 0 :(得分:0)

也许您可以将按钮更改为图像 然后你可以在按下按钮时创建动画

这是一个例子

Bitmap source0 = BitmapFactory.decodeResource(getResources(), R.drawable.top);
Bitmap source1 = BitmapFactory.decodeResource(getResources(), R.drawable.top_a);
Bitmap source2 = BitmapFactory.decodeResource(getResources(), R.drawable.top_b);
Bitmap source3 = BitmapFactory.decodeResource(getResources(), R.drawable.top_c);
v.startAnimation(AnimationUtils.loadAnimation(mainContext, R.anim.image_click));
toptop.setImageBitmap(source0);
top_a.setImageBitmap(processingBitmap_Brightness(source1));
top_b.setImageBitmap(source2);
top_c.setImageBitmap(source3);

将此代码放在ontouch事件上 然后v.startanimation用于触摸图像时的动画 然后我们将所选的图像视图设置为突出显示,并将亮度设置为比其他图像更亮

这是anim.image_click代码

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.5"
android:duration = "300">
</alpha>
<scale
android:fromXScale = "1"
android:toXScale = "0.9" 
android:fromYScale = "1"
android:toYScale = "0.9" 
android:pivotX="50%"
android:pivotY="50%" 
android:duration = "50">
</scale>
</set>

这是procssingBitmap_Brightness(用于处理图像亮度)

private Bitmap processingBitmap_Brightness(Bitmap src){
            Bitmap dest = Bitmap.createBitmap(
              src.getWidth(), src.getHeight(), src.getConfig());

            for(int x = 0; x < src.getWidth(); x++){
             for(int y = 0; y < src.getHeight(); y++){
              int pixelColor = src.getPixel(x, y);
              int pixelAlpha = Color.alpha(pixelColor);

              int pixelRed = Color.red(pixelColor) + brightnessValue;
              int pixelGreen = Color.green(pixelColor) + brightnessValue;
              int pixelBlue = Color.blue(pixelColor) + brightnessValue;

              if(pixelRed > 255){
               pixelRed = 255;
              }else if(pixelRed < 0){
               pixelRed = 0;
              }

              if(pixelGreen > 255){
               pixelGreen = 255;
              }else if(pixelGreen < 0){
               pixelGreen = 0;
              }

              if(pixelBlue > 255){
               pixelBlue = 255;
              }else if(pixelBlue < 0){
               pixelBlue = 0;
              }

              int newPixel = Color.argb(
                pixelAlpha, pixelRed, pixelGreen, pixelBlue);

              dest.setPixel(x, y, newPixel);

             }
            }
            return dest;
           }
相关问题