I am trying to draw a simple line from two points, but it does not work and it looks so simple following the previous examples on the web. I have created a new project and simple activity with the drawline code.

Could any one advise what is the problem with the code:

activity_main_screen.kt

package com.example.sandpit11
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.AttributeSet
import android.view.View
import kotlinx.android.synthetic.main.activity_main_screen.*


 class MainScreen : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main_screen)

    // --------------------------------------
    SettingImageButton.setOnClickListener {
        val intent = Intent(this, SettingActivity::class.java)

        startActivity(intent)
    }

    val bitmap = Bitmap.createBitmap(700, 1000, Bitmap.Config.ARGB_4444)
    val canvas = Canvas(bitmap)

    // Initialise a new Paint instance to draw the line
    val paint = Paint()
    paint.color = Color.WHITE
    canvas.drawLine(10f, 100f, 4000f, 400f, paint)

    // Line
    val paint2 = Paint()
    paint2.color = Color.GREEN
    paint2.strokeWidth = 10f
    val startx = 50f
    val starty = 100f
    val endx = 150f
    val endy = 210f
    canvas.drawLine(startx, starty, endx, endy, paint2)



}
}

activity_main_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainScreen"
    android:background="@android:color/black">

<ImageButton
        android:layout_width="86dp"
        android:layout_height="69dp"
        app:srcCompat="@drawable/settingicon"
        android:id="@+id/SettingImageButton"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintHorizontal_bias="0.987"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintVertical_bias="0.0"/>

 </android.support.constraint.ConstraintLayout>

1 Answers

0
Synergi On

Can you elaborate on what you are trying to accomplish here? Are you attempting to draw the line on the activity itself or some other view?

As Mike M. said, you're only creating a Bitmap and Canvas and doing operations to them. You're not actually using the Bitmap or Canvas to do any proper drawing.

I'm going to answer assuming that you're attempting to draw a line on a view that is inside the activity. As such, you are going to need a class that extends the View class (this can be either an inner class of your activity or in a separate file). In this class you will need to override the onDraw(Canvas) method and do operations to the Canvas parameter. You'll still be able to draw bitmaps through the Canvas drawBitmap() function (see Android Canvas docs). I'll provide a short example snippet below (it'll be in java but should be easily translated into Kotlin).

class MyView extends View {
    public MyView(Context context){
        super(context);
        // other initialization stuff
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.drawLine(...);
        canvas.drawBitmap(...);
        ...
        // your other code
        ...
    }
}

Once you have this view class, you should be able to manually inflate it in the activity's layout xml file or programmatically inflate it if that fits your use case better. An example of keeping it in your xml is below.

<your.package.name.MyView
        android:id="@+id/my_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        ...
        other properties
        ... />

It seems you have an ImageButton in your xml and code. If this view element is what you are trying to draw onto, you can more or less do the same thing. You just need to extend an ImageButton rather than View (since ImageButton is-a View).