Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android UI(SeekBar)詳解

Android UI(SeekBar)詳解

編輯:關於Android編程

目錄:
1.SeekBar的應用場景
2.SeekBar的簡單使用與事件監聽
3.圖片資源自定義SeekBar+手機音量調節
4.xml繪制自定義SeekBar

1.SeekBar的應用場景
SeekBar的主要應用在音樂播放,視頻播放,或者對音量調節,屏幕亮度調節的一些操作中,讓用戶可以手動的去改變相應的值。

2.SeekBar的簡單使用與事件監聽
1.activity_main.xml



    
        

    

    

    


2.content_main.xml


    
    
    

3.主java類MainActivity.java
package com.example.seekbarui;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private SeekBar seekBar;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //隱藏Actionbar後將toolbar設置上去替換Actionbar
        setSupportActionBar(toolbar);
        //初始化seekbar,TextView
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        textView = (TextView) findViewById(R.id.result);
        //seekbar設置監聽
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            /*
            * seekbar改變時的事件監聽處理
            * */
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                textView.setText("當前進度:"+progress+"%");
                Log.d("debug",String.valueOf(seekBar.getId()));
            }
            /*
            * 按住seekbar時的事件監聽處理
            * */
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast.makeText(MainActivity.this,"按住seekbar",Toast.LENGTH_SHORT).show();
            }
            /*
            * 放開seekbar時的時間監聽處理
            * */
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(MainActivity.this,"放開seekbar",Toast.LENGTH_SHORT).show();
            }
        });

        //浮動button實例化
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        //設置點擊監聽
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    //創建toolbar菜單
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    //toolbar菜單選擇監聽
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Toast.makeText(MainActivity.this,item.getTitle(),Toast.LENGTH_LONG).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

4.效果截圖

\

 

3.圖片資源自定義SeekBar+手機音量調節
1.導入滑動塊圖片(scrubber.png scrubber_focus.png),進度條就直接xml繪制
2.scrubber_selector.xml


    
    

3.seekbar_layer.xml(進度條繪制)


    
    
    
        
            
            
        
    
    
    
        
            
                
                
            
        
    
    
    
        
            
                
                
            
        
    

4.主布局activity_second.xml



    
        

    
    


5.主布局java類SecondActivity.java
package com.example.seekbarui;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class SecondActivity extends AppCompatActivity {
    private SeekBar seekBar;
    private TextView textView;
    private AudioManager audioManager;
    private int currentVolume,maxVolume;
    private int volume=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        //初始化seekbar,TextView
        seekBar = (SeekBar) findViewById(R.id.seekBar2);
        //初始化AudioManager
        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        //獲取系統最大音量值
        maxVolume =audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
        //將seekBar的最大進度值設置成系統最大音量
        seekBar.setMax(maxVolume);
        //獲取當前音量值
        currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
        //設置當前值為進度條當前值
        seekBar.setProgress(currentVolume);

        //初始化Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
        //隱藏Actionbar後將toolbar設置上去替換Actionbar
        setSupportActionBar(toolbar);
        //seekbar設置監聽
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            /*
            * seekbar改變時的事件監聽處理
            * */
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //設置拖動的進度值為系統音量
                audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM,progress,0);
                //獲取當前音量
                currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
                //設置當前音量
                seekBar.setProgress(currentVolume);
/*                //獲取屏幕屬性
                WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
                //設置屏幕亮度
                layoutParams.screenBrightness =((float)seekBar.getProgress())/100;
                Log.d("debug",layoutParams.screenBrightness+"");
                Log.d("deb",seekBar.getProgress()+"");
                //更新窗口屬性
                getWindow().setAttributes(layoutParams);*/
            }
            /*
            * 按住seekbar時的事件監聽處理
            * */
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast.makeText(SecondActivity.this,"按住seekbar",Toast.LENGTH_SHORT).show();
            }
            /*
            * 放開seekbar時的時間監聽處理
            * */
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(SecondActivity.this,"放開seekbar",Toast.LENGTH_SHORT).show();
            }
        });
    }

    //創建toolbar菜單
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    //toolbar菜單選擇監聽
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Toast.makeText(SecondActivity.this,item.getTitle(),Toast.LENGTH_LONG).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

6.效果截圖

圖片分別為更改前系統音量值,Seekbar效果圖,更改後系統音量值

\\\


4.xml繪制自定義SeekBar
上面3進度條的繪制已經運用了xml繪制,下面我們就試著xml繪制一下進度條的滑塊

1.未按壓時的xml繪制scrubber_shape.xml

    
        
        
        
    


2.按壓時的xml繪制scrubber_focus_shape.xml


    
    
    

3.效果截圖

\

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved