在视图类的Android载入一个布局xml文件 [英] load a layout xml file in a View class android
问题描述
我有这个类:
public class MainActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//LOAD MY LAYOUT
}
}
和更多code(显然)。
而且我的下一个界面:
And more code (obviously). And I have the next UI:
和我要画一条线,以模拟图像之间的联合。
And I want to draw a line to simulate the union between images.
我试着用:
Canvas canvas = new Canvas();
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawLine(0, 0, 300, 700, paint);
(以上只是一个测试,知道如果我能画一条线。)
但我不能画任何东西,因为当我创建一个这样的画布我的活动它没有连接到任何东西,我只是一个表面,切勿在屏幕的一部分上绘制。
(The above are just a test to know if I can draw a line.) But I can't draw anything because when I create a canvas like that in my activity it's not attached to anything, I'm just drawing on a surface that is never part of the screen.
因此,我有以下的类。
Therefore, I have the following classes.
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Paint mPaint;
float Mx1,My1;
float x,y;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView view1 =new MyView(this);
setContentView(view1);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeWidth(10);
}
public class MyView extends View {
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public MyView(Context c) {
super(c);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event){
float x = event.getX();
float y = event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
}
这是我发现了一个例子。
在视图类中的方法:
This is a example I found. In a view class the method:
setContentView(R.layout.main);
不起作用。
我的问题是:我怎么能在扩展视图类加载我的UI(我已经设置为图像)?因为在这个例子中我只是加载一个空白屏幕。我不知道如何加载我的布局file.xml
My question is: How I can load my UI (which I have set as an image) in the extended View class? because in the example I just load a empty screen. I don't know how to load my layout file.xml
感谢您的帮助!
推荐答案
我想我解决我的问题。
我伸出了LinearLayout中,而不是View类。
I extended a LinearLayout instead of View class.
public class MyView extends LinearLayout{
public MyView(Context c) {
super(c);
LayoutInflater inflater = (LayoutInflater) c.getSystemService(c.LAYOUT_INFLATER_SERVICE);
addView(inflater.inflate(R.layout.union, null));
}
}
有了这个,我可以载入我的布局。现在,我会试着画线。我希望这将是有用的其他人同样的问题。
With this I can load my layout. Now, I'll try to draw lines. I hope this will be usefull for other people with the same problem.
(编辑):
但是现在我不能画线。
我尝试在扩展活动类的下一个code:
(Edit): But now I can't draw a line. I try with the next code in the extended activity class:
Canvas canvas = new Canvas();
MyView.onDraw(canvas);
和我有扩展的LinearLayout类下一个code:
And I have the next code in the extended LinearLayout class:
public void onDraw(Canvas canvas) {
canvas.drawLine(0, 0, 9900, 9900, paint);
}
我可以画线,整个UI下?
I may draw the line under the whole UI?
((编辑再次))
我发现我的解决方案:
((Edit Again)) I found my solution:
public void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
canvas.drawLine(0, 0, 9900, 9900, paint);
}
如果我称之为下的德UI中的onDraw方法我划清界限,但如果我叫dispatchDraw()我上面画用户界面了一条线,我可以看到该行。
我希望这会为其他人非常有用的。
If I call a onDraw method I draw a line "under" de UI, but if I call dispatchDraw() I draw a line above user interface and I can see the line. I hope this will very useful for other people.
这篇关于在视图类的Android载入一个布局xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!