我是Android新手。我为我的项目构建了一个绘图应用程序,我想知道你们是否可以就我的问题提出建议。这是我的代码。
paintclass.java
public class paintclass extends View {
//InputStream is = getResources().openRawResource(R.drawable.pic);
private static final float TOUCH_TOLERANCE = 4;
static Paint paint;
static Path path = new Path();
//private Paint cpaint;
//private Path cpath = new Path();
static int[] color = {255, 0, 0, 0};
static int[] stroke = {5, 20};
static boolean eraser = false;
public LayoutParams params;
Bitmap bitmap;
LinearLayout parentLinearLayout;
LinearLayout parentLinearLayout2;
LinearLayout ll;
private Canvas mcanvas;
private float mX, mY;
public paintclass(Context context) {
super(context);
bitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
mcanvas = new Canvas(bitmap);
}
public paintclass(Context context, AttributeSet a) {
super(context, a);
bitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
mcanvas = new Canvas(bitmap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = new Paint();
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setARGB(color[0], color[1], color[2], color[3]);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
if (eraser == true) {
paint.setStrokeWidth(stroke[1]);
} else {
paint.setStrokeWidth(stroke[0]);
}
paint.setDither(true);
paint.setStrokeCap(Paint.Cap.ROUND);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bitmap, 0, 0, paint);
canvas.drawPath(path, paint);
invalidate();
}
private void touch_start(float x, float y) {
path.reset();
path.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) {
path.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touch_up() {
path.lineTo(mX, mY);
mcanvas.drawPath(path, paint);
path.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
// super.onTouchEvent(event);
float a = event.getX();
float b = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(a, b);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(a, b);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/liner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffcc"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff9900"
android:orientation="horizontal">
<ImageView
android:id="@+id/prima"
android:layout_width="47dp"
android:layout_height="47dp"
android:src="@drawable/pic" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="44dp"
android:layout_marginLeft="10dp"
android:singleLine="false"
android:text="Temporary text"
android:textAppearance="?android:attr/textAppearanceSearchResultTitle"
android:textSize="13sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffcc"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<ImageButton
android:id="@+id/bl"
android:layout_width="36dp"
android:layout_height="wrap_content"
android:src="@drawable/bl" />
<ImageButton
android:id="@+id/r"
android:layout_width="36dp"
android:layout_height="wrap_content"
android:src="@drawable/r" />
<ImageButton
android:id="@+id/b"
android:layout_width="36dp"
android:layout_height="wrap_content"
android:src="@drawable/b" />
<ImageButton
android:id="@+id/y"
android:layout_width="36dp"
android:layout_height="wrap_content"
android:src="@drawable/y" />
<ImageButton
android:id="@+id/o"
android:layout_width="36dp"
android:layout_height="wrap_content"
android:src="@drawable/o" />
<ImageButton
android:id="@+id/g"
android:layout_width="36dp"
android:layout_height="wrap_content"
android:src="@drawable/g" />
<ImageButton
android:id="@+id/p"
android:layout_width="36dp"
android:layout_height="wrap_content"
android:src="@drawable/p" />
<ImageButton
android:id="@+id/ad"
android:layout_width="36dp"
android:layout_height="wrap_content"
android:src="@drawable/ad" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:background="#cccccc"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<LinearLayout
android:id="@+id/drawingAre"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<com.depictry.paintclass xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/draw"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/addsize"
style="?android:attr/buttonStyleInset"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="5dp"
android:text="+" />
<Button
android:id="@+id/minussize"
style="?android:attr/buttonStyleInset"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="5dp"
android:text="-" />
<Button
android:id="@+id/clear"
style="?android:attr/buttonStyleInset"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="5dp"
android:layout_weight="0.10"
android:text="Clear" />
<Button
android:id="@+id/saveimage"
style="?android:attr/buttonStyleInset"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="5dp"
android:layout_weight="0.08"
android:text="Send" />
</LinearLayout>
</LinearLayout>
MainActivity
but[i] = (Button) findViewById(R.id.saveimage);
but[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) findViewById(R.id.drawingAre);
ImageView im = (ImageView) findViewById(R.id.prima);
ll.setDrawingCacheEnabled(true);
ll.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(ll.getDrawingCache());
ll.setDrawingCacheEnabled(false);
im.setImageBitmap(b);
}
});
这里的代码是我希望将我的图像上传到服务器文件夹的地方,但它只是显示图像。有人可以帮忙吗?