我正在创建一个应用程序,当服务启动时,它将打开一个聊天头,如视图叠加(图像)。但是当图像非常大而我无法改变尺寸时,我能够实现这一目标:
onCreate in service:
public void onCreate() {
super.onCreate();
prefs = new PreferenceHelper(getApplicationContext());
buildGoogleApiClient();
Log.i(LOGSERVICE, "onCreate");
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.floaticon);
int width = 20; // ((display.getWidth()*20)/100)
int height = 20;// ((display.getHeight()*30)/100)
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
chatHead.setLayoutParams(parms);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//Specify the view position
params.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
params.x = 0;
params.y = 20;
//Add the view to the window
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(chatHead, params);
chatHead.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//remember the initial position.
initialX = params.x;
initialY = params.y;
//get the touch location
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
//Calculate the X and Y coordinates of the view.
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
//Update the layout with new X & Y coordinate
mWindowManager.updateViewLayout(chatHead, params);
return true;
}
return false;
}
});
}
答案 0 :(得分:0)
Change your code from:
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
chatHead.setLayoutParams(parms);
to
ViewGroup.LayoutParams params = chatHead.getLayoutParams();
params.width = 20;
params.height = 20;
chatHead.setLayoutParams(PARMS);