Обрезать левый край Android ImageView - PullRequest
0 голосов
/ 04 мая 2018

enter image description here

Я пытаюсь получить результат выше, где изображение повернуто на 30 градусов, я пробовал это

thumbnailView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            thumbnailView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            thumbnailView.setPivotX(thumbnailView.getWidth() * 8 / 9);
            thumbnailView.setPivotY(0);
            thumbnailView.setRotation(30);
        }
    });

enter image description here

К сожалению, это не тот результат, которого я хочу, если кто-то просто подскажет, как мне этого добиться, я буду очень благодарен с помощью canvas или с помощью кода, или я должен создать custim ImageView или некоторые другие предложения?

layout.xml

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <ImageView
        android:id="@+id/progress_thumbnail"
        android:layout_width="@dimen/view_work_item_image_width"
        android:layout_height="match_parent"
        tools:ignore="ContentDescription"/>

        <CheckBox
            android:id="@+id/check_box_favorite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:background="@android:color/transparent"
            android:button="@drawable/favorite_checkbox"
            android:paddingEnd="4dp"
            android:paddingTop="15dp"/>

        <TextView
            android:id="@+id/progress_title"
            android:layout_width="@dimen/view_work_item_progress_bar_size"
            android:layout_height="@dimen/view_work_item_progress_bar_size"
            android:layout_alignParentEnd="true"
            android:ellipsize="end"
            android:gravity="center"
            android:layout_below="@+id/check_box_favorite"
            android:layout_marginEnd="10dp"
            android:lines="1"
            android:maxLines="1"
            android:singleLine="true"
            android:textColor="#007DD6"
            android:textStyle="bold"/>

        <ProgressBar
            android:id="@+id/grading_progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="@dimen/view_work_item_progress_bar_size"
            android:layout_height="@dimen/view_work_item_progress_bar_size"
            android:layout_alignStart="@+id/progress_title"
            android:layout_alignTop="@+id/progress_title"
            android:background="@drawable/summary_tile_progress_background"
            android:max="100"
            android:progress="50"
            android:progressDrawable="@drawable/summary_tile_progress_bar"/>

        <TextView
            android:id="@+id/text_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toStartOf="@id/progress_title"
            android:layout_toEndOf="@id/progress_thumbnail"
            android:ellipsize="end"
            android:layout_centerVertical="true"
            android:lines="3"
            android:maxLines="3"
            android:paddingStart="2dp"
            android:paddingEnd="1dp"
            tools:text="@string/lorem_ispum_short"
            android:textAppearance="@style/WorkItemTitleText"/>

        <TextView
            android:id="@+id/text_author_sharedate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_toStartOf="@id/work_item_action_container"
            android:ellipsize="end"
            android:paddingStart="2dp"
            android:paddingBottom="5dp"
            tools:text="@string/dummy_date"
            android:textAppearance="@style/TileAuthorText"/>

        <LinearLayout
            android:id="@+id/work_item_action_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:orientation="horizontal"
            android:layoutDirection="locale"
            android:paddingEnd="5dp"
            android:paddingStart="10dp"
            android:paddingTop="10dp"
            android:paddingBottom="5dp"
            android:layout_marginBottom="-5dp">

            <TextView
                android:id="@+id/workitem_continue_action"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:ellipsize="end"
                android:gravity="center_vertical"
                android:text="@string/workitem_continue"
                android:fontFamily="sans-serif-condensed"
                android:textColor="#494748"
                android:textSize="16sp"
                android:textStyle="bold"/>

            <ImageView
                android:id="@+id/workitem_continue_action_image"
                android:layout_width="20dp"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/continue_grading"/>

        </LinearLayout>

    </RelativeLayout>

Ответы [ 2 ]

0 голосов
/ 05 мая 2018

Хорошо, тогда вы должны сделать следующее:

Сначала вы должны рассмотреть возможность использования ConstraintLayout. Там вы можете легко выровнять правую сторону вашего изображения по левой границе вашего макета. Как это:

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/progress_thumbnail"
        android:layout_width="@dimen/view_work_item_image_width"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/yourSource"
        android:adjustViewBounds="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

(К сожалению, я не знаю, как сделать то же самое в RelativeLayout.)

Тогда вы можете сделать Вращение в вашей Деятельности следующим образом:

getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this);

        final double aspectRatio = (double)thumbnailView.getWidth()/thumbnailView.getHeight();

        final int newHeight = getWindow().getDecorView().getHeight()*9/8;
        final int newWidth = (int)(newHeight*aspectRatio);

        thumbnailView.getLayoutParams().height = newHeight;
        thumbnailView.getLayoutParams().width = newWidth;
        thumbnailView.requestLayout();

        thumbnailView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
           @Override
           public void onGlobalLayout() {
               thumbnailView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

               thumbnailView.setPivotX(newWidth);
               thumbnailView.setPivotY(newHeight);

               thumbnailView.setRotation(30);

           }
       });
    }
});    

Кажется немного сложным, но это должно сработать.

0 голосов
/ 04 мая 2018

Вы можете использовать атрибут rotation в вашем макете следующим образом:

<YourView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:rotation="30"/>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...