大家好:
我想做android的習字版
讓使用者可以寫字練習
我現在已經弄出畫板了
但遇到一些問題
(1)我希望我的畫板是100X100就好,不要占全部的介面(我其他地方還要放單字)
vBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
我都設定了還是全版面.....
(2)我想做浮水印,但目前研究不出來浮水印怎麼做
想做如下圖
以下是程式碼:MainActivity
public class MainActivity extends AppCompatActivity {
Panel mPanel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPanel = new Panel(this);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setContentView(mPanel);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
panel.java
class Panel extends View {
List<PointF> points=new ArrayList<PointF>();
Bitmap vBitmap;
Canvas vBitmapCanvas;
Paint mpaint = new Paint();
public Panel(Context context) {
super(context);
mpaint.setColor(Color.BLUE);
mpaint.setStyle(Paint.Style.STROKE);
mpaint.setStrokeWidth(10);
//設定bitmap大小
vBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
vBitmapCanvas = new Canvas(vBitmap);
vBitmapCanvas.drawColor(Color.WHITE);
}
@Override
public void onDraw (Canvas canvas){
super.onDraw(canvas);
for (int i = 1; i < points.size(); i++) {
PointF p1 = points.get(i - 1);
PointF p2 = points.get(i);
if(abs(p1.x-p2.x)<50 && abs(p1.y-p2.y)<50) {
canvas.drawLine(p1.x, p1.y, p2.x, p2.y, mpaint);
vBitmapCanvas.drawLine(p1.x, p1.y, p2.x, p2.y, mpaint);
}
}
}
@Override
public boolean onTouchEvent (MotionEvent event){
for (int i = 0; i < event.getHistorySize(); i++) {
points.add(new PointF(event.getHistoricalX(i), event.getHistoricalY(i)));
}
Panel.this.invalidate();
return true;
}
}
進度更新:
報告個為大大,我已經可以調正畫板大小,接下來就是想怎麼讓畫板有浮水印了...
若有經驗的大大拜託提供意見和參考,謝謝~
大小可能要在Xml父層元件設定,
而且每個手機的Pixel不一樣,
比較不建議用固定的Pixel來當作單位.
這只是我個人看法,
不代表專業立場.