Showing posts with label how can you Drawing on lyout. Show all posts
Showing posts with label how can you Drawing on lyout. Show all posts

how can you Drawing on lyout

package com.myandroid.kidsapp
Activity Name : Drawing.java
Target set :2.1

in this application have two java file and on xml that paint.xml

no need to take any permission into manifest.xml file
//////////////////////////////////////////////////////////////////
1. Drawing .java (main activity )


public class Drawing extends Activity implements OnTouchListener {

SignaturePad paint_llayout;
LinearLayout initpad1 ;
Button Btsave,btreset ,btSave;
ImageButton tvSeeting;
private int pos;

EditText inputFileName;

final String[] items = {
             "Red", "Green",
             "Blue","Black"
      };


String fileName;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paint);
    initpad1=(LinearLayout)findViewById(R.id.linearLayout2_paint_paint);
        btreset=(Button)findViewById(R.id.btReset_paint);
        btreset.setOnTouchListener(this);
       
        btSave=(Button)findViewById(R.id.btSave_paint);
        btSave.setOnTouchListener(this);
       
        tvSeeting=(ImageButton)findViewById(R.id.img_setting_paint);
        tvSeeting.setOnTouchListener(this);
       

 
        // set canvas ///
       
      setSignatureCanvas();
     
      /////////////////
       
       
       
    }
       
        public boolean onTouch(View v, MotionEvent event)
    {
    switch(event.getAction())
    {
        case  MotionEvent.ACTION_DOWN :
    switch(v.getId())
      {
        case R.id.btReset_paint:
     
    break;
        case R.id.btSave_paint:
         
        break;
           }
                 break;
     case  MotionEvent.ACTION_UP :
          switch(v.getId())
            {
                 case R.id.btReset_paint :
                 paint_llayout.discard();
       
             break;
           
                 case R.id.img_setting_paint :
               
                 // alert Dialog for pic color Name //
               
                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setIcon(R.drawable.piccolor_sml);
                 builder.setTitle("Pick a color");
                           
                  builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener()
                        {  
                      public void onClick(DialogInterface dialog, int item)
                      {  
                            pos=item;
                            SignaturePad.mPaint.setColor(0xFF000000);
                            Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                        }}).setPositiveButton("Ok", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

//* set color value as per select * //    

if(items[pos].equalsIgnoreCase("Red"))
{
   
       SignaturePad.mPaint.setColor(0xFFaa0000);
}
     else if(items[pos].equalsIgnoreCase("Green"))
  {
        SignaturePad.mPaint.setColor(0xFF00aa00);
  }
else if(items[pos].equalsIgnoreCase("Blue"))
 {
    SignaturePad.mPaint.setColor(0xFF0000aa);
   }
    else if(items[pos].equalsIgnoreCase("Black"))
      {
SignaturePad.mPaint.setColor(0xFF000000);
}
    else
    {
    SignaturePad.mPaint.setColor(0xFF000000);
    }

}
}).setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
     }
  });
                  builder.show();
             
             break;
     
                 case R.id.btSave_paint:
               
               
                 AlertDialog.Builder alert1 = new AlertDialog.Builder(this);
                 alert1.setTitle(" Save ");
                 alert1.setMessage("Put file name ");
                 alert1.setIcon(R.drawable.save_sml);

                 // Set an EditText view to get user input
               
                 inputFileName= new EditText(this);
               
                 alert1.setView(inputFileName);

                 alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int whichButton) {
                               
                    fileName=inputFileName.getText().toString();
                   
                   if(fileName==null)
                    {
                      paint_llayout.save("default.PNG");
                    Toast.makeText(Drawing.this, "you must provied file name ? ", Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                    paint_llayout.save(fileName+".PNG");
                    Toast.makeText(Drawing.this, " file "+fileName, Toast.LENGTH_LONG).show();
                    }
                 
            }
                 });

                 alert1.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int whichButton) {
                   
                   }
                 });

                 alert1.show();
               
                             
               break;
          }
     break;
     
    }
   
    return false;
   
       
         
   
    }
   
  private void setSignatureCanvas() {
     
    paint_llayout = new SignaturePad(Drawing.this);
initpad1.addView(paint_llayout.getSignaturePad());

}

////////////////////////////////////////////////////////////////
2.SignaturePad.java

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


public class SignaturePad extends View {

Context _context;
public static Paint   mPaint;
@SuppressWarnings("unused")
private MaskFilter  mEmboss;
@SuppressWarnings("unused")
private MaskFilter  mBlur;
private String filename;

private View v1;
MyView v;
private String storagePath = Environment.getExternalStorageDirectory().toString();

 
public SignaturePad(Context context) {
super(context);
_context = context;
v = new MyView(context);      
v1 = v.getRootView();
       
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFF000000);
             
     
        // mPaint.setColor(0xFFaa0000);
       
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);
        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
                                       0.4f, 6, 3.5f);
        mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

}



public View getSignaturePad()
{
//filename=filename1;
return v;
}

public void colorChanged(int color) {
        mPaint.setColor(color);
    }
public void discard(){
//mPaint.setARGB(0xff,125,125,125);
//v.mCanvas.drawPaint(mPaint);
v.mBitmap.eraseColor(0xFFAAAAAA);
v.invalidate();
}

    public class MyView extends View {

        @SuppressWarnings("unused")
private static final float MINP = 0.25f;
        @SuppressWarnings("unused")
private static final float MAXP = 0.75f;

        private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;

        public MyView(Context c,int col) {
            super(c);

            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        }

        public MyView(Context c) {
            super(c);

            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        }
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            if(w > 0 && h > 0){
           mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
           mCanvas = new Canvas(mBitmap);
            }
        }

        @Override
        protected void onDraw(Canvas canvas) {
        // set backround color//
            canvas.drawColor(0xFFAAAAAA);

           
            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);
            // commit the path to our offscreen
            mCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw
            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 save(String fName){
   
    filename=fName;
   
        System.out.println("Root View : "+v1);
        v1.setDrawingCacheEnabled(true);
        Bitmap bm = v1.getDrawingCache();
        System.out.println("Bitmap : "+bm);
       // String filename = "sign.png";
try {
OutputStream fOut = null;
File file = new File(storagePath, filename);
fOut = new FileOutputStream(file);
if(bm != null)
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(_context.getContentResolver(), file
.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
Toast.makeText(_context,
"Exception at save: File not found" + e.getMessage(),
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(_context,
"Exception at save: IO stream error" + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
//Toast.makeText(_context, "Saved to: " + storagePath + "/" + filename,
//Toast.LENGTH_SHORT).show();
       
   }


}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
And main.xml file is :
in this case it is paint.xml



<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="79dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btReset_paint"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="0.02"
            android:text="Reset"
            android:visibility="visible" />



         <ImageButton
             android:id="@+id/img_setting_paint"
             android:layout_width="wrap_content"
             android:layout_height="fill_parent"
             android:layout_weight="0.02"
             android:background="#000000"
             android:text="Setting"
             android:visibility="visible" android:src="@drawable/piccolor"/>
       
        <Button
            android:id="@+id/btSave_paint"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="0.03"
            android:text="Save"/>

    </LinearLayout>




    <LinearLayout
        android:id="@+id/linearLayout2_paint_paint"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.08"
        android:background="#ffffff"
        android:orientation="vertical" >

    </LinearLayout>

</LinearLayout>

/////////////////////////////////////////////////////////////////////////////////////////////////////////

now you can run the aaplication .................







IRCTC Share Price Declines by 2% Despite 30% Jump in Q4 Net Profit; Board Announces Dividend of INR 2 per Share

Introduction: The share price of Indian Railway Catering and Tourism Corporation (IRCTC) experienced a decline of 2% in today's trading ...