本文介绍了满足条件时,如何以编程方式更改ImageButton src目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个学校项目,正在尝试启动手电筒应用程序.对于开/关ImageButton,我要有4个自定义图像.

I have a school project where I am trying to get a flashlight app going. For the on/off ImageButton, I want to have 4 custom images.

如果手电筒关闭:-turn_on.png(默认)-turn_on_pressing.png(按下状态=真)

If flashlight is off:-turn_on.png (default)-turn_on_pressing.png (state pressed = true)

如果手电筒打开:-turn_off.png(默认)-turn_off_pressing.png(按下状态=真)

If flashlight is on:-turn_off.png (default)-turn_off_pressing.png (state pressed = true)

我希望将ImageButton src目标更改为"on_selector.xml"和"off_selector.xml"之间.最初,我能够setImageResource()更改默认按钮图像.现在,我想添加自己的印刷图像,所以我现在很困难.

I am hoping to change the ImageButton src target to change between "on_selector.xml" and "off_selector.xml". Originally I was able to setImageResource() to change the default button images. And now that I am trying to add my own on press images, I am having a very difficult time.

Application.java:

Application.java:

import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.view.View;
import android.widget.ImageButton;
import com.jrr.flashlight.R;

public class Application extends Activity {

    ImageButton flashControl;
    Camera camera;
    Parameters params;

    private boolean hasFlash;
    private boolean isFlashOn = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.application_layout);

        flashControl = (ImageButton) findViewById(R.id.flashButtonOn);
        flashControl.setOnClickListener(flashControlListener());

        hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);  //sets value for boolean hasFlash

    }


    private View.OnClickListener flashControlListener() {
        return new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if (!isFlashOn){
                    findViewById(R.id.appbackground).setBackgroundColor(Color.WHITE);

                    if(hasFlash){
                        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                        camera.setParameters(params);
                        camera.startPreview();
                    }
                    isFlashOn = true;
                } else {
                    findViewById(R.id.appbackground).setBackgroundColor(Color.BLACK);

                    if(hasFlash){
                        params.setFlashMode(Parameters.FLASH_MODE_OFF);
                        camera.setParameters(params);
                        camera.stopPreview();
                    }
                    isFlashOn = false;
                }

            }
        };
    }

    @Override
    protected void onResume() {
        super.onResume();
        camera = Camera.open();
        params = camera.getParameters();

    }

    @Override
    protected void onPause() {
        super.onPause();

        if(camera!=null) {
            camera.release();
            camera = null;
        }
        findViewById(R.id.appbackground).setBackgroundColor(Color.BLACK);
    }

application_layout.xml:

application_layout.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/appbackground"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="0dp"
              android:layout_margin="0dp"
              android:background="#000000">

    <ImageButton
            android:background="#80000000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            <!--I want to be able to change the following line-->
            android:src="@drawable/on_selector"
            android:id="@+id/flashButtonOn" android:layout_gravity="center"/>
</LinearLayout>

on_selector.xml:

on_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/turn_on_pressing" /> <!-- pressed -->
    <item android:drawable="@drawable/turn_on" /> <!-- default -->
</selector>

off_selector.xml:

off_selector.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/turn_off_pressing" /> <!-- pressed -->
    <item android:drawable="@drawable/turn_off" /> <!-- default -->
</selector>

我甚至想做些什么,还是有另一种方法可以做到这一点?我已经坚持了大约3个小时,无法在任何地方在线找到答案(也许不是在问正确的问题?),所以我们将不胜感激!

Is what I want to do even possible or is there another way to accomplish this? I have been stuck on this for about 3 hours and cannot find the answer online anywhere (maybe not asking the right question?) so any help is appreciated!

谢谢!

推荐答案

是否要求它必须是imageResource?您可以尝试使用backgroundResource.

Is it a requirement that it has to be imageResource? You can try using backgroundResource instead.

ImageButton flashButtonOn = (ImageButton) findViewById(R.id.flashButtonOn);
flashButtonOn.setBackgroundResource(R.drawable.on_selector);

或尝试

flashButtonOn.setImageResource(R.drawable.replacementGraphic);

这篇关于满足条件时,如何以编程方式更改ImageButton src目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:58