使用颜色选择器(选择颜色)和搜索栏(以不同尺寸书写)在应用程序上进行创建。我已成功实现了上述所有功能,但我的问题是无法将画布全屏显示,当我将画布设置为全屏时,我无法添加颜色选择器和搜索栏。
主要布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<com.colorpiker.ColorPicker
android:id="@+id/picker"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="left" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:background="@android:color/transparent"
android:max="40" />
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
</LinearLayout>
<LinearLayout
android:id="@+id/signatureView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
活动
public class MainActivity extends Activity {
private SignatureView _signView ;
private SeekBar seekBar;
private Button save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
_signView = new SignatureView(getApplicationContext(),"");
save = (Button)findViewById(R.id.save);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try
{
long imgp = ActivityCamera.imagename;
String imageName = String.valueOf(imgp);
Bitmap well = _signView.getBitmap();
String fileName = "/mnt/sdcard/Pictures/Worpie/"+imageName + ".jpg";
OutputStream os = new FileOutputStream(fileName);
well.compress(Bitmap.CompressFormat.PNG, 80, os);
os.flush();
os.close();
}catch (NullPointerException e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Null error", Toast.LENGTH_SHORT).show();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "File error", Toast.LENGTH_SHORT).show();
}
catch (IOException e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "IO error", Toast.LENGTH_SHORT).show();
}
}
});
seekBar = (SeekBar) findViewById(R.id.seekBar1);
seekBar.setOnSeekBarChangeListener(
//http://stackoverflow.com/questions/16163215/android-styling-seek-bar
new OnSeekBarChangeListener() {
int progress = 10;
@Override
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
progress = progresValue;
}`enter code here`
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
writeText(progress);
}
});
ColorPicker picker = (ColorPicker) findViewById(R.id.picker);
picker.getColor();
picker.setOldCenterColor(picker.getColor());
picker.setOnColorChangedListener(new OnColorChangedListener() {
@Override
public void onColorChanged(int color) {
_signView.setNewColor(color);
}
});
_signView.setNewColor(picker.getColor());
LinearLayout _layout = (LinearLayout)findViewById(R.id.signatureView);
_layout.addView(_signView);
}
//============================
public void writeText(float stroke){
_signView.setStroke(stroke);
}
}
SignatureView
package com.colorpiker;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class SignatureView extends View {
private Bitmap mBitmap, mybitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;
private float stroke;
private String imageName;
// 3 Constructors, covers instantiating manually or from a layout file
public SignatureView(Context context, String imageName) {
super(context);
this.imageName = imageName;
initSignatureView();
}
public SignatureView(Context context, AttributeSet attrs) {
super(context, attrs);
initSignatureView();
}
public SignatureView(Context context, AttributeSet attrs, int defaultStyle) {
super(context, attrs, defaultStyle);
initSignatureView();
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
protected void initSignatureView() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
}
public void setStroke(float stroke) {
mPaint.setStrokeWidth(stroke);
}
public void setNewColor(int color) {
mPaint.setColor(color);
}
@Override
public void onDraw(Canvas canvas) {
if (mBitmap == null) {
long imgp = ActivityCamera.imagename;
String imageName = String.valueOf(imgp);
mybitmap = BitmapFactory.decodeFile("/mnt/sdcard/Pictures/Worpie/"+ imageName + ".jpg");
mBitmap = Bitmap.createScaledBitmap(mybitmap, getMeasuredWidth(),getMeasuredHeight(), false);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); // draw offscreen
canvas.drawPath(mPath, mPaint); // draw current path
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
mCanvas.drawPath(mPath, mPaint);// commit the path to our offscreen
mPath.reset();// kill this so we don't double draw
}
public Bitmap getBitmap()
{
this.setDrawingCacheEnabled(true);
this.buildDrawingCache();
Bitmap bmp = Bitmap.createBitmap(this.getDrawingCache());
this.setDrawingCacheEnabled(false);
return bmp;
}
}