1.总体思想
活用padding和margin
2.实现过程
1 public class PopupShredderView extends FrameLayout{ 2 public PopupShredderView(Context context, AttributeSet attrs, int defStyle) { 3 4 super(context, attrs, defStyle); 5 init(); 6 } 7 8 public PopupShredderView(Context context, AttributeSet attrs) { 9 10 super(context, attrs); 11 init() ; 12 } 13 14 public PopupShredderView(Context context) { 15 16 super(context); 17 init(); 18 } 19 20 21 private void init(){ 22 final LayoutInflater layoutInflater = LayoutInflater.from(context); 23 mView = layoutInflater.inflate(R.layout.popup_shredder_clean, this, false); 24 25 mCleanText1.setImageDrawable(themeManager.getDrawable(R.drawable.exit_clean_date)); 26 mCleanText2.setBackgroundDrawable(themeManager.getDrawable(R.drawable.exit_clean_result)); 27 mCleanText2.setVisibility(View.INVISIBLE); 28 } 29 30 public void update(int step, int offset){ 31 if (step == STEP_END) { 32 return; 33 } 34 int clean1Offset = mCleanText1.getMeasuredHeight() * step / STEP_END; 35 int maxClean1Offset=mCleanText1.getMeasuredHeight(); 36 if (offset + clean1Offset < maxClean1Offset) { 37 clean1Offset = clean1Offset + offset; 38 } else { 39 clean1Offset = maxClean1Offset; 40 } 41 mCleanText1.setPadding(0, clean1Offset, 0, 0); 42 mCleanText2.setVisibility(View.VISIBLE); 43 int clean2Offset = ((View) mCleanText2.getParent()).getMeasuredHeight() * (STEP_END - step) 44 / STEP_END; 45 int maxClean2Offset= 0; 46 if (clean2Offset - offset > maxClean2Offset) { 47 clean2Offset = clean2Offset - offset; 48 } else { 49 clean2Offset = maxClean2Offset; 50 } 51 LinearLayout.LayoutParams layoutParams = (android.widget.LinearLayout.LayoutParams) mCleanText2 52 .getLayoutParams(); 53 layoutParams.bottomMargin = clean2Offset; 54 mCleanText2.setLayoutParams(layoutParams); 55 invalidate(); 56 } 57 58 }
3.布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/menubar" 4 android:layout_width="192dp" 5 android:layout_height="wrap_content" 6 android:layout_gravity="center" 7 android:orientation="vertical" > 8 9 <ImageView 10 android:id="@+id/exit_clean1" 11 android:layout_width="wrap_content" 12 android:layout_height="@dimen/popup_shredder_top_heigh" 13 android:layout_gravity="center_horizontal" 14 android:layout_marginLeft="21dp" 15 android:contentDescription="@string/app_name" 16 android:scaleType="matrix" /> 17 18 <LinearLayout 19 android:layout_width="wrap_content" 20 android:layout_height="@dimen/popup_shredder_top_heigh" 21 android:layout_marginLeft="28dp" 22 android:layout_marginTop="@dimen/popup_shredder_bottom_top_margin" 23 android:orientation="vertical" > 24 25 <ImageView 26 android:id="@+id/exit_clean2" 27 android:layout_width="wrap_content" 28 android:layout_height="match_parent" 29 android:layout_gravity="center_horizontal" 30 android:contentDescription="@string/app_name" /> 31 </LinearLayout> 32 33 <ImageView 34 android:id="@+id/exit_clean3" 35 android:layout_width="wrap_content" 36 android:layout_height="@dimen/popup_shredder_delete_heigh" 37 android:layout_gravity="center_horizontal" 38 android:layout_marginTop="@dimen/popup_shredder_delete_top_margin" 39 android:contentDescription="@string/app_name" /> 40 41 </RelativeLayout>