In Android what does the dimension %p mean?
I've been looking through some of 开发者_如何学JAVAthe Android examples on the developer website, and I've seen %p
being used as a dimension.
I've done a Google search, but can't find any information about what it means. Does anyone know?
Check out the keyWidth documentation for an explanation.
the optional %p suffix provides a size relative to some parent container
- 50% is the centre of the animated view (relative to view).
- 50%p is the centre of the view’s parent (relative to parent).
- 50 is 50 pixels from the top/left of the animated view (absolute).
Answer of @Cheryl Simon and @zero_cool is correct.
And I add the demo for easy to understand
(The below gray view height is 200dp
, not 400dp
)
Layout code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/button_animate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animate"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="// A gray view with 200dp height"
android:layout_marginTop="20dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#aaa"
android:orientation="horizontal"
>
<TextView
android:layout_width="60dp"
android:layout_height="50dp"
android:background="#f00"
android:text="50dp height View"
android:layout_marginStart="10dp"
/>
<TextView
android:id="@+id/image_1"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:background="#f00"
android:src="@mipmap/ic_launcher"
android:text="animate with %"
/>
<TextView
android:id="@+id/image_2"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:background="#f00"
android:text="animate with %p"
/>
</LinearLayout>
</LinearLayout>
Animation file
slide_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="5000"
android:fromYDelta="25%"
android:toYDelta="0"/>
</set>
slide_in_with_p.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="5000"
android:fromYDelta="25%p"
android:toYDelta="0"/>
</set>
Activity code
button_animate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation slide = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_in);
Animation slideWithP = AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_in_with_p);
viewAnimateWithPercent.startAnimation(slide);
viewAnimateWithPercentP.startAnimation(slideWithP);
}
});
DEMO
精彩评论