我创建了一个浮动按钮,未连接到任何活动或视图组 (将其直接添加到窗口管理器中),我希望在触摸时增长和缩小。 但是,缩放它超过1会裁剪我想要显示的图像。 如何使imageview增长以匹配内部重新缩放的图像?
这是一些相关的代码:
创建AnimatedButton,它在服务类OnCreate中扩展ImageView (AnimatedButton扩展了ImageView,只是在创建OnDraw时在图像上添加了一些额外的绘图, 不要认为它的代码在这里是相关的):
public void onCreate(){ super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mFloatingButton = new AnimatedButton(this);
mFloatingButton.setImageResource(R.drawable.ax);
...
...
params= new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
//this code is for dragging the chat head
mFloatingButton.setOnTouchListener(new View.OnTouchListener() {
...
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
...
case MotionEvent.ACTION_UP:
if (isAClick(initialTouchX, event.getRawX(), initialTouchY, event.getRawY())) {
...
ObjectAnimator anim = ObjectAnimator.ofFloat(mFloatingButton, "Scale", 1.0f, 3.0f);
anim.setDuration(700);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(1);
...
}
...
}
...
}
...
});
windowManager.addView(mFloatingButton, params);
...
}
这是AnimatedButton的setScale:
public void setScale(float scale) {
mScale = scale;
this.setScaleX(mScale);
this.setScaleY(mScale);
invalidate();
}
TNX