How to reduce and shift the view to the upper left corner?

I have a layout resource file:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <!-- other widget here -->

  <ImageView
    android:id="@+id/thumbnail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFFFF"
    android:layout_gravity="top|left"
    android:visibility="gone"/>

</FrameLayout>

At a certain point in time, this layout will be shown in my work. At this point, I set the visibility thumbnail ImageViewto View.VISIBLE, and then I want to revive it so that it occupies 30% of the width / height of the screen, located marginPixelsfrom the top left corner (on my test device it marginPixelsis 48). This will open the base View, with the exception of the part accepted thumbnail.

To this end, I have the following Java:

thumbnail.setVisibility(View.VISIBLE);
thumbnail
  .animate()
  .scaleX(0.3f)
  .scaleY(0.3f)
  .x(marginPixels)
  .y(marginPixels)
  .setDuration(animDuration);

As a result, animation occurs, but thumbnailis in the center FrameLayout.

I tried:

  • Both have and don't have android:layout_gravity="top|left"on ImageViewin the layout xml

  • translationX(marginPixels)/ translationY(marginPixels)instead of x(marginPixels)/y(marginPixels)

  • translationXBy(marginPixels)/ translationYBy(marginPixels)instead of x(marginPixels)/y(marginPixels)

, , . thumbnail . x() y(), , , - . scaleX() scaleY() ( x() y(), ), thumbnail , - , . setX() setY() thumbnail x() y(), thumbnail , , .

, , , . , x()/y() ViewPropertyAnimator, , .

UPDATE. (, , ), , ):

int x=(int)(-0.35f*(float)bmp.getWidth())+marginPixels;
int y=(int)(-0.35f*(float)bmp.getHeight())+marginPixels;

thumbnail.setVisibility(View.VISIBLE);
thumbnail.setImageBitmap(bmp);
thumbnail
  .animate()
  .x(x)
  .y(y)
  .scaleX(0.3f)
  .scaleY(0.3f)
  .setDuration(animDuration);

, , . IOW, x y ?

+4
1

scale ( setScale, ) pivot. pivotX pivotY width height (). , pivots View ( ) View, scale - 1.0f.

X Y ( translationX translationY) pivots. , thumbnail marginPixels ( , ).

, , , .

thumbnail.setVisibility(View.VISIBLE);
thumbnail.setPivotX(0);
thumbnail.setPivotY(0);
thumbnail
  .animate()
  .scaleX(0.3f)
  .scaleY(0.3f)
  .x(marginPixels)
  .y(marginPixels)
  .setDuration(animDuration);

+3

Source: https://habr.com/ru/post/1621998/


All Articles