我有一个学校项目,我试图让手电筒应用程序运行。对于开/关 ImageButton,我想要 4 个自定义图像。
如果手电筒关闭:-turn_on.png(默认)-turn_on_pressing.png(按下状态 = true)
如果手电筒打开:-turn_off.png(默认)-turn_off_pressing.png(按下状态 = true)
我希望将 ImageButton src 目标更改为在“on_selector.xml”和“off_selector.xml”之间进行更改。最初我能够setImageResource()
更改默认按钮图像。现在我想在新闻图片上添加我自己的,我有一个非常困难的时期。
应用程序.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>
我想要做的甚至是可能的还是有其他方法可以做到这一点?我已经坚持了大约 3 个小时,并且无法在任何地方在线找到答案(也许没有问正确的问题?)所以任何帮助表示赞赏!
谢谢!