问题
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.
If flashlight is off: -turn_on.png (default) -turn_on_pressing.png (state pressed = true)
If flashlight is on: -turn_off.png (default) -turn_off_pressing.png (state pressed = true)
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:
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:
<?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:
<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:
<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>
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!
Thanks!
回答1:
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);
Or try
flashButtonOn.setImageResource(R.drawable.replacementGraphic);
回答2:
It should be "setImageResource", "setBackgroundResource" changes the background, not the src. btn_MyButton.setImageResource(R.drawable.button_bg_selector_collapsed);
回答3:
When setting the image using the following attribute:
android:src="@drawable/on_selector"
You will need to use the following to change the image dynamically:
ImageButton myButton = (ImageButton) findViewById(R.id.my_button);
myButton.setImageResource(R.drawable.my_button_image);
来源:https://stackoverflow.com/questions/17129589/how-do-i-programmatically-change-the-imagebutton-src-target-when-a-condition-is