Ahmad Alkurdi
Ahmad Alkurdi

Reputation: 11

adding View class to the default layout

I'm new to Android development. I have a class DrawView that extends View, it's like a simple paint app

public class DrawView extends View implements OnTouchListener {

    Bitmap bitmap;
    Canvas bitmapCanvas;
    int color;

    // Position of finger down
    float pX, pY;

    // Position of finger up
    float mX, mY;

    // Create new path
    Path path = new Path();

    // Is view initialized?!
    boolean isInitialized;

    // Create new paint
    Paint paint = new Paint();

    int begX, begY, endX, endY = 0;         


    //DrawView constructor
    public DrawView(Context context) {
            // Initialize new view
            super(context);
            setFocusable(false);
            setFocusableInTouchMode(false);
            this.setOnTouchListener(this);
            requestLayout();

            paint.setAntiAlias(true);
            paint.setStyle(Style.STROKE);

            // There is no bitmap yet
            isInitialized = false;
    }

    //Initialize bitmap and canvas
    private void init() {

            bitmap = Bitmap.createBitmap(getWidth(), getHeight()/2, Bitmap.Config.RGB_565);
            bitmap.setPixel(72, 72, Color.BLACK);

            // Create new canvas and set bitmap
            bitmapCanvas = new Canvas();
            bitmapCanvas.setBitmap(bitmap);

            // ... set canvas background color
            bitmapCanvas.drawColor(Color.WHITE);

            // We're done with initialization
            isInitialized = true;
    }

    //Reset canvas
    public void reset(){
            bitmapCanvas.drawColor(Color.WHITE);  
    }

    //Handle event 'onDraw'
    @Override
    public void onDraw(Canvas canvas) {

            // Check if initialized
            if (!isInitialized)
                    init();

            // Draw bitmap!
            canvas.drawBitmap(bitmap, 0, 0, paint);
    }

    //Handle event 'onTouch'        
    public boolean onTouch(View view, MotionEvent event) {
            // Check event type

            switch (event.getAction()) {

            // Finger down
            case MotionEvent.ACTION_DOWN:

                    paint.setColor(Color.BLACK);
                    paint.setStrokeWidth(7f);
                    paint.setStrokeJoin(Paint.Join.ROUND);
                    paint.setStrokeCap(Paint.Cap.ROUND);
                    paint.setAntiAlias(true);

                    // Get current position
                    pX = event.getX();
                    pY = event.getY();                      

                    // Set beginning of path to (posX,posY)                     
                    path.moveTo(pX, pY);
                    begX= (int) pX;
                    begY = (int) pY;
                    bitmapCanvas.drawPoint(pX, pY, paint);

                    break;

            // Finger moves
            case MotionEvent.ACTION_MOVE:
                    mX = event.getX();
                    mY = event.getY();

                    // Set position of end of path
                    path.lineTo(mX, mY);
                    endX = (int) mX;
                    endY = (int) mY;

                    // Draw path
                    bitmapCanvas.drawPath(path, paint);

                    // Invalidate canvas (redraw the view)

                    invalidate();
                    break;

            // Finger up
            case MotionEvent.ACTION_UP:

                    mX = event.getX();
                    mY = event.getY();

                    if (mY == pY && mX == pX){                      
                        bitmapCanvas.drawPoint(pX, pY, paint);
                        invalidate();
                    }

                    path.reset(); 
                    break;

            }

            return true;
    }
}

In my default layout, I want it to contains DrawView and other elements (button, textview ...)

How can I split "default layout" to contains DrawView with these elements? Thanks

edit: Where should I put this code to make DrawView work correctly

public class Draw extends Activity {
DrawView drawView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    drawView = new DrawView(this);
    setContentView(drawView);
    drawView.requestFocus();
}
}

Upvotes: 0

Views: 1340

Answers (2)

Pratik
Pratik

Reputation: 30855

I don't know how you want actually but you can use any layout as per your requirement here is the simple layout which your your view and another views in split screen

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1">

    <TextView /> <!--your textview-->
    <TextView /> <!--your textview-->
    <TextView /> <!--your textview-->
</LinearLayout>

<com.androidapp.DrawView android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="1"/>

</LinearLayout>

This will split the screen in equal size for your view and another view with nested LinearLayout

If you want to display your content with wrap size then just remove the weight if you display at top of your DrawView and use wrap_content in height or if you display at bottom than give them weight in 0.2 or o.3 whatever but in float

Edit Define both constructor

public DrawView(Context context) {
        // Initialize new view
        super(context);
        initObject();
}

public MyCustomView(Context context, AttributeSet attrs){
    super(context, attrs);
    initObject();
}

private void initObject(){
      setFocusable(false);
      setFocusableInTouchMode(false);
      this.setOnTouchListener(this);
      requestLayout();

      paint.setAntiAlias(true);
      paint.setStyle(Style.STROKE);

      // There is no bitmap yet
      isInitialized = false;
}

Upvotes: 0

hasanghaforian
hasanghaforian

Reputation: 14022

Add your view with it's full name to your layout.For example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<TextView.../>
<my.package.MyCustomView

    android:id="@+id/my_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<Button.../>
</LinearLayout>

Here MyCustomView is name of java class that extends View and my.package is name of package that contains that class.You can see that you can add LayoutParams to it,as I added layout_height or layout_width.

Edit:

If you want to use your custom view in XML layout,you have to add at least this constructor to your class:

public MyCustomView(Context context, AttributeSet attrs){
    super(context, attrs);
    ...
}

Upvotes: 1

Related Questions