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