Scale view of parent layout size

I am trying to scale the view to fit the layout using an object animator. Presentation - LinearLayout . The view is stretched, but not to the size of the screen in both directions (i.e., X and Y).

Here is the code.

I feel that either the problem is this:

  • Formula for calculating how many teeth need to be made.

     zoomTillX = screen_width/zoomView_width; zoomTillY = screen_height/zoomView_height; 
  • Or with the Animation property code, which does not execute correctly.

Please let me know how I can zoom.

 public class MainActivity extends AppCompatActivity { TextView tv; double screen_height; LinearLayout zoomView; double screen_width; double zoomTillX; double zoomTillY; double zoomView_width; double zoomView_height; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); zoomView = (LinearLayout) findViewById(R.id.zoomView); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); screen_height = (double)dm.heightPixels; screen_width = (double)dm.widthPixels; zoomView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { zoomView.getViewTreeObserver().removeOnGlobalLayoutListener(this); zoomView_width = (double)zoomView.getMeasuredWidth(); zoomView_height = (double)zoomView.getMeasuredHeight(); } }); zoomView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final Handler handler = new Handler(Looper.getMainLooper()); handler.postDelayed(new Runnable() { @Override public void run() { if(zoomView_width > 0 && zoomView_height > 0) { zoomTillX = screen_width/zoomView_width; zoomTillY = screen_height/zoomView_height; Log.d("VIEW GET X IS ",String.valueOf(zoomView.getX())); Log.d("VIEW GET Y IS ",String.valueOf(zoomView.getY())); ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(zoomView, "scaleX", (float)(zoomTillX)); ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(zoomView, "scaleY",(float)(zoomTillY)); List<Animator> oaList = new ArrayList<Animator>(); oaList.add(scaleDownX); oaList.add(scaleDownY); AnimatorSet ani = new AnimatorSet(); ani.playTogether(oaList); ani.setDuration(500); ani.start(); }else{ handler.postDelayed(this,300); } } },500); } }); } } 

Here's how it looks at last.

enter image description here

+5
source share
1 answer

This can be done using ValueAnimator .

The presence of this layout as activity content:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/view" android:layout_width="170dp" android:layout_height="170dp" android:background="#3143ff"/> </FrameLayout> 

And in onCreate() action:

 final View view = findViewById(R.id.view); final View contentView = findViewById(R.id.content_frame); contentView.setOnClickListener(v -> { final int screenWidth = contentView.getWidth(); final int screenHeight = contentView.getHeight(); ValueAnimator widthAnimator = ValueAnimator.ofInt(view.getWidth(), screenWidth); ValueAnimator heightAnimator = ValueAnimator.ofInt(view.getHeight(), screenHeight); widthAnimator.setDuration(1500); heightAnimator.setDuration(1500); widthAnimator.addUpdateListener(animation -> { view.getLayoutParams().width = (int) animation.getAnimatedValue(); view.requestLayout(); }); heightAnimator.addUpdateListener(animation -> { view.getLayoutParams().height = (int) animation.getAnimatedValue(); view.requestLayout(); }); widthAnimator.start(); heightAnimator.start(); });
final View view = findViewById(R.id.view); final View contentView = findViewById(R.id.content_frame); contentView.setOnClickListener(v -> { final int screenWidth = contentView.getWidth(); final int screenHeight = contentView.getHeight(); ValueAnimator widthAnimator = ValueAnimator.ofInt(view.getWidth(), screenWidth); ValueAnimator heightAnimator = ValueAnimator.ofInt(view.getHeight(), screenHeight); widthAnimator.setDuration(1500); heightAnimator.setDuration(1500); widthAnimator.addUpdateListener(animation -> { view.getLayoutParams().width = (int) animation.getAnimatedValue(); view.requestLayout(); }); heightAnimator.addUpdateListener(animation -> { view.getLayoutParams().height = (int) animation.getAnimatedValue(); view.requestLayout(); }); widthAnimator.start(); heightAnimator.start(); }); 

This will be the result:

enter image description here


Referral API

We ourselves have implemented this animation. But why don't we let the system take care of creating all these animators?

There Transitions API , which will take on a heavy lift. All we need to do is ask the framework to detect layout changes, create appropriate animators, and run animations.

So, all of the above code can be changed to the following, which will lead to an exact exit:

 contentView.setOnClickListener(v -> { final int screenWidth = contentView.getWidth(); final int screenHeight = contentView.getHeight(); // Uncomment this, if you want Transitions API to run default animation // TransitionManager.beginDelayedTransition(contentView); Transition autoTransition = new AutoTransition(); autoTransition.setDuration(1500); // With this overload you can control actual transition animation TransitionManager.beginDelayedTransition(contentView, autoTransition); // After `beginDelayedTransition()` function perform changes to the layout // Transitions framework will detect those changes and perform appropriate animations view.getLayoutParams().width = screenWidth; view.getLayoutParams().height = screenHeight; view.requestLayout(); view.invalidate(); });
contentView.setOnClickListener(v -> { final int screenWidth = contentView.getWidth(); final int screenHeight = contentView.getHeight(); // Uncomment this, if you want Transitions API to run default animation // TransitionManager.beginDelayedTransition(contentView); Transition autoTransition = new AutoTransition(); autoTransition.setDuration(1500); // With this overload you can control actual transition animation TransitionManager.beginDelayedTransition(contentView, autoTransition); // After `beginDelayedTransition()` function perform changes to the layout // Transitions framework will detect those changes and perform appropriate animations view.getLayoutParams().width = screenWidth; view.getLayoutParams().height = screenHeight; view.requestLayout(); view.invalidate(); }); 
+6
source

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


All Articles