Android
 Computer >> コンピューター >  >> プログラミング >> Android

Kotlinを使用してAndroidで線を引く方法は?


この例は、Kotlinを使用してAndroidで線を引く方法を示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:id="@+id/relativeLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <ImageView
      android:id="@+id/imageView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true" />
   <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="70dp"
      android:text="Draw Line" />
</RelativeLayout>

ステップ3 −次のコードをsrc / MainActivity.kt

に追加します
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
   lateinit var button: Button
   lateinit var imageView: ImageView
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      button = findViewById(R.id.button)
      imageView = findViewById(R.id.imageView)
      button.setOnClickListener {
         val bitmap = Bitmap.createBitmap(10, 700, Bitmap.Config.ARGB_8888)
         val canvas = Canvas(bitmap)
         canvas.drawColor(Color.RED)
         val paint = Paint()
         paint.color = Color.RED
         paint.style = Paint.Style.STROKE
         paint.strokeWidth = 8F
         paint.isAntiAlias = true
         val offset = 50
         canvas.drawLine(
         offset.toFloat(), (canvas.height / 2).toFloat(), (canvas.width - offset).toFloat(),          (canvas.height /
         2).toFloat(), paint)
         imageView.setImageBitmap(bitmap)
      }
   }
}

ステップ4 −次のコードをandroidManifest.xmlに追加します

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、[実行]アイコンをクリックします ツールバーからKotlinを使用してAndroidで線を引く方法は? 。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します

Kotlinを使用してAndroidで線を引く方法は?


  1. C ++を使用してOpenCVで線を引く方法は?

    線を引くには、始点と終点の2つの点が必要です。線を引くためのキャンバスも必要です。 キャンバスのマトリックスであるOpenCVを使用して、ラインの開始点と終了点を定義する必要があります。線にも色を付ける必要があります。線の太さも説明する必要があります。 OpenCVを使用して線を描画する場合は、マトリックス、2つのポイント、および色と線の太さを宣言する必要があります。 OpenCVを使用するには、 を含める必要があります line()のためのヘッダー 関数はこのヘッダーで定義されています。 このメソッドの基本的な構文は次のとおりです- 構文 line(whiteMatrix, star

  2. Javaを使用してOpenCVで線を引く方法は?

    Java OpenCVライブラリのorg.opencv.imgprocパッケージには、Imgprocという名前のクラスが含まれています。線を引くには、 line()を呼び出す必要があります このクラスのメソッド。このメソッドは、次のパラメーターを受け入れます- 線を引く画像を表すマットオブジェクト。 線が引かれるポイントを表す2つのPointオブジェクト。 線の色を表すScalarオブジェクト。 (BGR) 線の太さを表す整数(デフォルト:1)。 例 import org.opencv.core.Core; import org.opencv.core.Mat; i