Reputation: 346
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!!!
Upvotes: 1
Views: 1912
Reputation: 24423
Do you create the layout file name "main.xml" in your project? If not, create it first.
Upvotes: 0
Reputation: 346
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.
Upvotes: 3
Reputation: 1461
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
Upvotes: 0