2

이런 간단한 응용 프로그램을 만들려고했지만 실패했습니다. answer을 기반으로해야 할 작업은 무엇입니까? 코드 내가 사용화면 필터처럼 밝기를 줄이는 방법

MainActivity.java

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     SeekBar fseekbar = (SeekBar) findViewById(R.id.fseekBar2); 
     fseekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){ 

    @Override 
    public void onProgressChanged(SeekBar seekBar, int progress, 
     boolean fromUser) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStartTrackingTouch(SeekBar seekBar) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onStopTrackingTouch(SeekBar seekBar) { 
     // TODO Auto-generated method stub 
    } 
} 

내 상대 Layout.xml

<LinearLayout 
     android:id="@+id/linear" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

    <SeekBar 
     android:id="@+id/fseekBar2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 

</LinearLayout> 

문제는 내가 윈도우 매니저를 생성 할 수 있다는 것입니다 밝기의 값을의 LayoutParams 설정 .

답변

1

이 시도 ...

MainActivity.java

public class MainActivity extends Activity { 
private SeekBar brightbar; 

//Variable to store brightness value 
private int brightness; 
//Content resolver used as a handle to the system's settings 
private ContentResolver cResolver; 
//Window object, that will store a reference to the current window 
private Window window; 

TextView txtPerc; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //Instantiate seekbar object 
    brightbar = (SeekBar) findViewById(R.id.brightbar); 

    txtPerc = (TextView) findViewById(R.id.txtPercentage); 

    //Get the content resolver 
    cResolver = getContentResolver(); 

    //Get the current window 
    window = getWindow(); 

    //Set the seekbar range between 0 and 255 
    brightbar.setMax(255); 
    //Set the seek bar progress to 1 
    brightbar.setKeyProgressIncrement(1); 

    try 
    { 
     //Get the current system brightness 
     brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS); 
    } 
    catch (SettingNotFoundException e) 
    { 
     //Throw an error case it couldn't be retrieved 
     Log.e("Error", "Cannot access system brightness"); 
     e.printStackTrace(); 
    } 

    //Set the progress of the seek bar based on the system's brightness 
    brightbar.setProgress(brightness); 

    //Register OnSeekBarChangeListener, so it can actually change values 
    brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
    { 
     public void onStopTrackingTouch(SeekBar seekBar) 
     { 
      //Set the system brightness using the brightness variable value 
      System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness); 
      //Get the current window attributes 
      LayoutParams layoutpars = window.getAttributes(); 
      //Set the brightness of this window 
      layoutpars.screenBrightness = brightness/(float)255; 
      //Apply attribute changes to this window 
      window.setAttributes(layoutpars); 
     } 

     public void onStartTrackingTouch(SeekBar seekBar) 
     { 
      //Nothing handled here 
     } 

     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
     { 
      //Set the minimal brightness level 
      //if seek bar is 20 or any value below 
      if(progress<=20) 
      { 
       //Set the brightness to 20 
       brightness=20; 
      } 
      else //brightness is greater than 20 
      { 
       //Set brightness variable based on the progress bar 
       brightness = progress; 
      } 
      //Calculate the brightness percentage 
      float perc = (brightness /(float)255)*100; 
      //Set the brightness percentage 
      txtPerc.setText((int)perc +" %"); 
     } 
    }); 
} 
} 

main.xml에

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Slide the bar to change the brightness:" 
    android:id="@+id/txtPercentage" /> 

<SeekBar 
    android:id="@+id/brightbar" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="30dip" 
    android:layout_marginRight="30dip" > 
</SeekBar> 

</LinearLayout> 

해피 코딩 ...

+0

이 매니페스트 이 권한을 설정하십시오

관련 문제