简体   繁体   中英

load a layout xml file in a View class android

I have this class:

public class MainActivity extends Activity{
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);//LOAD MY LAYOUT
    }
}

And more code (obviously). And I have the next UI:

在此输入图像描述

And I want to draw a line to simulate the union between images.

I tried with:

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);

doesn't work.

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

Thanks for your help!!!

I think I solve my problem.

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.

(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);

And I have the next code in the extended LinearLayout class:

public void onDraw(Canvas canvas) {
            canvas.drawLine(0, 0, 9900, 9900, paint);
}

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);
}

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.

Try to put your first approach in the Activity.onResume() method. The Activity should be on the foreground already. See the Activity lifecycle at http://developer.android.com/reference/android/app/Activity.html

Do you create the layout file name "main.xml" in your project? If not, create it first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM