作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用对象动画器将 View 缩放到布局大小。该 View 是一个 LinearLayout
。 View 会拉伸(stretch),但不会拉伸(stretch)到两个方向(即 X 和 Y)的屏幕尺寸。
这是代码。
我觉得要么是这个问题:
计算必须完成多少缩放的公式。
zoomTillX = screen_width/zoomView_width;
zoomTillY = screen_height/zoomView_height;
或者以错误的方式完成的 Animation 属性代码。
请告诉我如何实现放大。
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);
}
});
}
}
这是最终的样子。
最佳答案
这可以通过 ValueAnimator
完成.
将此布局作为 Activity 的内容:
<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>
在 Activity 的 onCreate()
中:
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();
});
这将是结果:
我们自己实现了这个动画。但为什么我们不让系统负责构建所有这些动画师呢?
有一个 Transitions API ,这将为我们带来繁重的工作。我们所要做的就是要求框架检测布局变化、创建适当的动画师并运行动画。
因此,上面的所有代码都可以更改为以下,这将导致完全相同的输出:
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();
});
关于android - 将 View 缩放到父布局大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44235527/
我有一个具有可变数量子元素的固定大小的 div。我不知道 children 的大小。目标是缩小它们以适合父级。 例子: .parent { width: 100px; height: 100p
我是一名优秀的程序员,十分优秀!