android canvas 編輯bitmap畫虛線
效果圖
佈局檔案:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testsss.PaintActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="畫線"
android:onClick="paint"/>
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>
activity
package com.example.testsss;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PathEffect;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class PaintActivity extends Activity {
private ImageView iv;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paint);
iv = (ImageView) findViewById(R.id.iv);
bitmap = Bitmap.createBitmap(getResources().getDisplayMetrics().widthPixels,getResources().getDisplayMetrics().heightPixels-iv.getTop(), Config.ARGB_8888);
}
public void paint(View v){
test();
}
private void test() {
Canvas canvas = new Canvas(bitmap);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setStyle(Style.STROKE);
p.setColor(Color.BLUE);
p.setStrokeWidth(5);
PathEffect effects = new DashPathEffect(new float[] { 1, 2, 4, 8}, 1);
p.setPathEffect(effects);
canvas.drawCircle(getResources().getDisplayMetrics().widthPixels/2,getResources().getDisplayMetrics().widthPixels/2,
getResources().getDisplayMetrics().widthPixels/3, p);
canvas.drawLine(0, 0, getResources().getDisplayMetrics().widthPixels,getResources().getDisplayMetrics().widthPixels, p);
iv.setImageBitmap(bitmap);
}
}
相關文章
- Android: Bitmap/Canvas/DrawableAndroidCanvas
- Bitmap回收—Canvas: trying to use a recycled bitmap android.graphicsCanvasAndroid
- Android 利用 Canvas 畫畫板AndroidCanvas
- canvas 虛線矩形Canvas
- canvas圖形編輯器Canvas
- Android粒子篇之Bitmap畫素級操作Android
- canvas小畫板--(1)平滑曲線Canvas
- canvas畫素畫板Canvas
- Android 畫圓弧canvas.drawArc() 詳解AndroidCanvas
- canvas進階——如何畫出平滑的曲線?Canvas
- 能夠輕鬆編輯畫報的圖片編輯軟體
- Android仿微信圖片編輯——塗鴉框架Doodle(多功能畫板)Android框架
- android圖片編輯,類似PC端QQ截圖後的編輯效果(畫箭頭,圓形,矩形標註)Android
- Android圖片編碼機制(Bitmap,Skia,libJpeg)Android
- 理解Android BitmapAndroid
- Android Bitmap 使用Android
- Android Bitmap 初探Android
- iOS/Android 影片編輯SDKiOSAndroid
- 編輯手記:精通AndroidAndroid
- 線性dp:編輯距離
- 用canvas 畫煙花Canvas
- canvas畫圖採坑Canvas
- canvas 畫進度條Canvas
- 瞭解canvas畫布Canvas
- 繪畫和照片編輯器:Artstudio Pro for MacMac
- Android豎虛線繪製Android
- Android中的BitmapAndroid
- Android Bitmap優化Android優化
- Android Bitmap面面觀Android
- Canvas簡歷編輯器-Monorepo+Rspack工程實踐CanvasMono
- Artstudio Pro for Mac 繪畫和照片編輯軟體Mac
- Artstudio Pro for Mac(繪畫和圖片編輯工具)Mac
- Tkinter (03) 畫布部件 CanvasCanvas
- canvas簡單畫板效果Canvas
- canvas畫布效果程式碼Canvas
- Canvas畫圖-滑鼠塗鴉Canvas
- html5畫布canvasHTMLCanvas
- 什麼是物理畫素、虛擬畫素、邏輯畫素、裝置畫素,什麼又是 PPI, DPI, DPR 和 DIP