//////Paint.java/////////////////
public class Paint extends PaintActivity implements
ColorPickerDialog.OnColorChangedListener {
private Paint mPaint;
private PaintView view;
private int wid;
private int wsize;
private int pref;
private int RESULT_TOGGLE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new PaintView(this);
setContentView(R.layout.paint_layout);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.parseColor("black"));
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
Button close = (Button) findViewById(R.id.closePaint);
close.setOnClickListener(new View.OnClickListener() {
public void onClick(View View) {
//save();
//updateWidgets();
setResult(RESULT_OK);
finish();
}
});
Button toggle = (Button) findViewById(R.id.togglePaint);
toggle.setOnClickListener(new View.OnClickListener() {
public void onClick(View View) {
//save();
if (pref == 0)
//launchText();
else
{
setResult(RESULT_TOGGLE);
finish();
}
}
});
}
else
{
//Problem - just bail
TextView tv = new TextView(this);
tv.setText("Error.");
setContentView(tv);
}
}
public class PaintView extends View {
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public PaintView(Context c) {
super(c);
initialize();
}
public PaintView(Context c, AttributeSet s) {
super(c,s);
initialize();
}
public void initialize() {
mBitmap = Bitmap.createBitmap(320, 430,
Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
public void loadImage() {
String path = "/sdcard/image.jpg";
Bitmap b = BitmapFactory.decodeFile(path);
Drawable d = new BitmapDrawable(b);
Bitmap bitmap = Bitmap.createBitmap(320, 430,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.setBounds(0, 0, 320, 430);
d.draw(canvas);
mBitmap = bitmap;
mCanvas = canvas;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int
oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
Log.w("onDraw","");
canvas.drawColor(Color.parseColor("white"));
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
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);
mPath.reset();
}
@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;
}
public void clear() {
mBitmap = Bitmap.createBitmap(320, 480,
Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
invalidate();
}
}
////////////END OF PaintView////////////////////
////////////CONTINUE Paint/////////////////////
private void save()
{
Bitmap b = Bitmap.createBitmap( view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas( b );
view.draw( c );
FileOutputStream fos = null;
try {
fos = new FileOutputStream( "/sdcard/image" + ".jpg" );
if ( fos != null )
{
b.compress(Bitmap.CompressFormat.PNG, 100, fos );
fos.close();
}
} catch( Exception e )
{
Log.e("Save View", "Exception: " + e.toString() );
}
}
public void colorChanged(int color) {
mPaint.setColor(color);
}
}
///////////////paint_layout.xml///////////////////
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:id="@+id/paint"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View class="com.app.name.Paint$PaintView"
android:id="@+id/PaintView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button android:id="@+id/closePaint"
android:text="Close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom" />
<Button android:id="@+id/togglePaint"
android:text="Toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom" />
</FrameLayout>
This is my current setup. Currently it just loads a black screen with
the buttons, no painting working. But I know the paint code works, it
stopped working when I tried customizing the view with
paint_layout.xml
On Jun 18, 1:21 am, Nithin <[email protected]> wrote:
> -"it does not work properly." means what. What error are you
> getting ?. Do you override all the constructors.
>
> Nithin
>
> On Jun 17, 9:54 pm, Matt M <[email protected]> wrote:
>
> > Hello,
>
> > My current painting activity is built off of FingerPaint.java from the
> > API Demos. In my xml I declared the custom view, see below. When I
> > setContentView(view); everything works fine, but the problem is when I
> > setContentView(paint_layout.xml) it does not work properly.
>
> > ///////////PaintActivity///////////
> > public class Paint extends PaintActivity implements
> > ColorPickerDialog.OnColorChangedListener {
>
> > private Paint mPaint;
> > private PaintView view;
>
> > �...@override
> > protected void onCreate(Bundle savedInstanceState) {
> > super.onCreate(savedInstanceState);
> > view = new PaintView(this);
> > setContentView(view);
>
> > //Set mPaint code
> > }
>
> > public class PaintView extends View {
> > //PaintView code
> > }
> > //Paint code
>
> > }
>
> > ///////////paint_layout.xml///////////
> > <FrameLayout xmlns:android="http://schemas.android.com/apk/res/
> > android"
> > android:id="@+id/paint"
> > android:layout_width="fill_parent"
> > android:layout_height="fill_parent">
>
> > <com.myname.appname.PaintView
> > android:id="@+id/PaintView"
> > android:layout_width="fill_parent"
> > android:layout_height="fill_parent" />
>
> > <!-- other buttons and text -->
> > </FrameLayout>
>
> > Can anyone inform me of why exactly? Am I missing an attribute for the
> > custom layout in the xml?
>
> > Thank you!
>
> > Matt.
>
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en