自定义进度条-笔记

s



android 2.3LayerDrawable失效

<?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="@dimen/action_bar_height"
    android:background="@drawable/action_bar_bg"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="@drawable/btn_back_bg_selector"
        android:contentDescription="@null"
        android:paddingLeft="@dimen/action_bar_btn_back_padding_left"
        android:paddingRight="@dimen/action_bar_btn_back_padding_right"
        android:src="@drawable/action_bar_back_icon" />

    <TextView
        android:id="@+id/progress"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="1dip"
        android:layout_weight="1"
        android:background="@color/action_bar_bg_color"
        android:contentDescription="@null"
        android:gravity="center"
        android:textColor="#838383"
        android:textSize="15sp" />

</LinearLayout>



    /**
     * 设置进度
     * 
     * @param num
     */
    public void setProgress(float currentProgress, final String text) {
        
        if (progress != null) {
            
            Resources res = getResources(); 
            float width = res.getDisplayMetrics().widthPixels - res.getDimension(R.dimen.action_bar_btn_back_width); 
//            float width = progress.getWidth(); //view未展示之前,getWidth的值为0
            
            final Drawable[] layers = new Drawable[2];
            layers [0] = new ColorDrawable(getResources().getColor(R.color.action_bar_with_progress_fg));
            layers [1] = new ColorDrawable(getResources().getColor(R.color.action_bar_bg_color));
            
            final LayerDrawable ld = new LayerDrawable(layers);
            ld.setLayerInset(1, (int)(currentProgress*width), 0, 0, 0);  
            
            progress.setBackgroundDrawable(ld);
            progress.setText(text);
            
        }
    }

所以改成这样:

<?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="@dimen/action_bar_height"
    android:background="@drawable/action_bar_bg"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="@drawable/btn_back_bg_selector"
        android:contentDescription="@null"
        android:paddingLeft="@dimen/action_bar_btn_back_padding_left"
        android:paddingRight="@dimen/action_bar_btn_back_padding_right"
        android:src="@drawable/action_bar_back_icon" />

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="1dip" >

        <ImageView
            android:id="@+id/progress"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/action_bar_with_progress_fg"
            android:contentDescription="@null"
            android:gravity="center" />

        <TextView
            android:id="@+id/progress_text"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:textColor="#838383"
            android:textSize="15sp" />
    </FrameLayout>

</LinearLayout>



    /**
     * 设置进度
     * 
     * @param num
     */
    public void setProgress(float currentProgress, final String text) {
        
        if (progress != null) {
            
            Resources res = getResources(); 
            float width = res.getDisplayMetrics().widthPixels - res.getDimension(R.dimen.action_bar_btn_back_width); 
//            float width = progress.getWidth(); //view未展示之前,getWidth的值为0
            
            setWidth(progress, (int) (currentProgress * width), true);

            progressText.setText(text);

        }
    }

    private void setWidth(View view, int width, boolean animate) {
        if (animate) {
            ResizeWidthAnimation anim = new ResizeWidthAnimation(view, width);
            anim.setDuration(1000);
            view.startAnimation(anim);
        } else {
            final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
            params.width = width;
            view.setLayoutParams(params);
        }
    }

package com.app.widget;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
 * 改变控件宽度(动态效果)
 * 
 * @author wangdw
 * @see http://stackoverflow.com/questions/16641539/animate-change-width-of-android-relativelayout
 */
public class ResizeWidthAnimation extends Animation {
    private int mWidth;
    private int mStartWidth;
    private View mView;

    public ResizeWidthAnimation(View view, int width) {
        mView = view;
        mWidth = width;
        mStartWidth = view.getWidth();
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        int newWidth = mStartWidth + (int) ((mWidth - mStartWidth) * interpolatedTime);

        mView.getLayoutParams().width = newWidth;
        mView.requestLayout();
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}




s




s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值