今天看啥
热点:

自定义圆形滚动条(在自定义标题栏中显示)--利用开源项目ProgressWheel(二),progresswheel


本篇是ProgressWheel使用的第二篇(尾篇),功能是在自定义标题栏中显示ProgressWheel滚动条。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/white"
    tools:context=".ProgressWheelTestActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

2) titlebar_detail.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ProgressWheel="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="@color/blue" >

    <ImageView
        android:id="@+id/imageview_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textview_middle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/home"
        android:textColor="@color/white"
        android:textSize="18sp" />

    <com.todddavies.components.progressbar.ProgressWheel
        android:id="@+id/progressBar"
        android:layout_width="46dp"
        android:layout_height="46dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        ProgressWheel:barColor="#0097D6"
        ProgressWheel:barLength="30dp"
        ProgressWheel:barWidth="5dp"
        ProgressWheel:contourColor="#330097D6"
        ProgressWheel:rimColor="@color/white"
        ProgressWheel:rimWidth="5dp"
        ProgressWheel:spinSpeed="10dp"
        ProgressWheel:text="0/0"
        ProgressWheel:textColor="#FFFFFF"
        ProgressWheel:textSize="12sp"
        android:visibility="gone"
         />

    <ImageView
        android:id="@+id/imageView_Right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"

        android:src="@drawable/search_normal"

        />

</RelativeLayout>

3)自定义的styles:(在styles文件中):

  <!-- customer define. -->
    <style name="CustomWindowTitleBackground">
        <item name="android:background">@color/honeydew</item>
    </style>

    <style name="test" parent="android:Theme">
        <item name="android:windowTitleSize">48dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>

4). java代码:ProgressWheelTestActivity.java

package com.example.progresswheeltest;

import com.todddavies.components.progressbar.ProgressWheel;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

public class ProgressWheelTestActivity extends Activity {

    ProgressWheel mProgressWheel =null;
    private ImageView imageView_Right;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_progress_wheel_test);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.titlebar_detail);

        //init layout
        ImageView imageView_Back = (ImageView)findViewById(R.id.imageview_back);
        imageView_Back.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                Toast.makeText(mContext, "left icon is pressed!", Toast.LENGTH_LONG).show();

            }

        });

        mProgressWheel = (ProgressWheel)findViewById(R.id.progressBar);
        mProgressWheel.setVisibility(View.GONE);

        imageView_Right = (ImageView)findViewById(R.id.imageView_Right);
        imageView_Right.setVisibility(View.GONE);

        //start test
        startTestHandler();


    }

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

    // //////////progress wheel//////
    private void showProgressWheel() {
        if (mProgressWheel != null) {
            mProgressWheel.setVisibility(View.VISIBLE);
            mProgressWheel.spin();
        }
    }

    private void closeProgressWheel() {
        if (mProgressWheel != null) {
            mProgressWheel.stopSpinning();
            mProgressWheel.setVisibility(View.GONE);
        }

        // show right icon
        if (imageView_Right != null) {
            imageView_Right.setVisibility(View.VISIBLE);
        }
    }

    private void setProgressWheelText(String text) {
        if (mProgressWheel != null) {
            mProgressWheel.setText(text);
        }
    }

    //////for test thread /////
    private void startTestHandler() {
        mMyHandler.start();
    }

    MyHandler mMyHandler = new MyHandler();
    private class MyHandler extends Handler {
        public int loop = 0;
        MyHandler() {
        }


        public void start() {
            showProgressWheel();
            sendEmptyMessage(200);
        }

        @Override
        public void handleMessage(Message msg) {

            if (msg.what == 200) {

                startTestThread();
            }else if (msg.what == 300) {
                //error
                closeProgressWheel();
            }else if (msg.what == 0){
                //结束
                closeProgressWheel();
            }

        }
    }

    /// for test thread,用thread来模拟
    private void startTestThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                mMyHandler.loop++;
                if (mMyHandler.loop<5) {
                    setProgressWheelText("" + mMyHandler.loop + "/" + 5);
                    mMyHandler.sendEmptyMessage(200);
                }else {
                    //结束时,发送300
                    mMyHandler.sendEmptyMessage(300);
                }

            }

        }).start();

    }
}

5)说明:

1. 需要导入ProgressWheel jar包或者以library的方式导入到项目中;
2. 自定义titlebar的实现(注意加载完成后右边的图片会更新);
3. 请区别本博ProgressWheel 使用的第一篇的实现方式,第一篇用的是静态的方式实现的。

开源的力量是无穷的!

www.bkjia.comtruehttp://www.bkjia.com/Androidjc/1002801.htmlTechArticle自定义圆形滚动条(在自定义标题栏中显示)--利用开源项目ProgressWheel(二),progresswheel 本篇是ProgressWheel使用的第二篇(尾篇),功能是在...

相关文章

帮客评论

视觉看点