如何绘制在屏幕的中间的ImageView? (编程) [英] how to draw a ImageView in the middle of the screen? (programatically)
本文介绍了如何绘制在屏幕的中间的ImageView? (编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要画一个简单的ImageView在屏幕的中部,水平和垂直方向。但我想这样做,而不使用XML文件,我需要以编程方式做到这一点。
我tryed下一个code,但它不工作正常,它绘制图像一点点的权利和一点点的底部。如何解决呢?
ARImage =新ImageView的(getApplicationContext());
ARImage.setImageResource(R.drawable.x);
rl.addView(ARImage); // RL是,它插入到一个帧布局相对布局 显示显示=((窗口管理器)getSystemService(Context.WINDOW_SERVICE))getDefaultDisplay()。
INT W = display.getWidth();
INT H = display.getHeight();
RelativeLayout.LayoutParams位置=新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
position.leftMargin =(int)的(W / 2);
position.topMargin =(int)的(H / 2);
ARImage.setLayoutParams(位置);
解决方案
这为我的作品是这样的:
包pete.android.study;进口android.app.Activity;
进口android.content.Context;
进口android.os.Bundle;
进口android.view.Display;
进口android.view.WindowManager;
进口android.widget.ImageView;
进口android.widget.RelativeLayout;
进口android.widget.RelativeLayout.LayoutParams;
公共类主要活动扩展{ / *
*(非Javadoc中)
* @see android.app.Activity#的onCreate(android.os.Bundle)
* /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
ImageView的ARImage =新ImageView的(getApplicationContext());
ARImage.setImageResource(R.drawable.icon);
RelativeLayout的RL =新的RelativeLayout(本);
显示显示=((窗口管理器)getSystemService(Context.WINDOW_SERVICE))getDefaultDisplay()。
INT W = display.getWidth();
INT H = display.getHeight();
RelativeLayout.LayoutParams位置=新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
ARImage.setLayoutParams(位置); position.addRule(RelativeLayout.CENTER_IN_PARENT);
rl.addView(ARImage,位置);
的setContentView(RL);
}}
I want to draw a simple ImageView in the middle of the screen, horizontally and vertically. But i want to do it without using XML files, i need to do it programatically.
I tryed with the next code, but it doesn't works fine, it draws the image a little to the right and a little to the bottom. How to solve it?
ARImage = new ImageView(getApplicationContext());
ARImage.setImageResource(R.drawable.x);
rl.addView(ARImage); //rl is the relative layout that it's inserted into a frame layout
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.leftMargin = (int)(w/2);
position.topMargin = (int)(h/2);
ARImage.setLayoutParams(position);
解决方案
It works for me like this:
package pete.android.study;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
public class Main extends Activity {
/*
* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView ARImage = new ImageView(getApplicationContext());
ARImage.setImageResource(R.drawable.icon);
RelativeLayout rl = new RelativeLayout(this);
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ARImage.setLayoutParams(position);
position.addRule(RelativeLayout.CENTER_IN_PARENT);
rl.addView(ARImage, position);
setContentView(rl);
}
}
这篇关于如何绘制在屏幕的中间的ImageView? (编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文