Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android what does the dimension %p mean?

I've been looking through some of the 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?

like image 470
Quentamia Avatar asked Dec 10 '10 18:12

Quentamia


3 Answers

Check out the keyWidth documentation for an explanation.

the optional %p suffix provides a size relative to some parent container

like image 66
Cheryl Simon Avatar answered Nov 02 '22 12:11

Cheryl Simon


  1. 50% is the centre of the animated view (relative to view).
  2. 50%p is the centre of the view’s parent (relative to parent).
  3. 50 is 50 pixels from the top/left of the animated view (absolute).
like image 45
zero_cool Avatar answered Nov 02 '22 12:11

zero_cool


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)

enter image description here

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

like image 33
Linh Avatar answered Nov 02 '22 12:11

Linh