Thursday, September 13, 2012

touch draw on Bitmap (android)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class MainActivity extends Activity implements OnTouchListener {

 ImageView imageView;
 Bitmap bitmap;
 Canvas canvas;
 Paint paint;

 float bx = 0;
 float by = 0;
 float dx = 0;
 float dy = 0;
 float ex = 0;
 float ey = 0;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

  Display currentDisplay = getWindowManager().getDefaultDisplay();
  float dw = currentDisplay.getWidth();
  float dh = currentDisplay.getHeight();

  bitmap = Bitmap.createBitmap((int) dw, (int) dh,Bitmap.Config.ARGB_8888);
  canvas = new Canvas(bitmap);
  paint = new Paint();
  paint.setColor(Color.BLACK);
        
  imageView = new ImageView(this);
  imageView.setImageBitmap(bitmap);
  imageView.setOnTouchListener(this);
  setContentView(imageView);
 }
    
    public boolean onTouch(View v, MotionEvent event) {
     int action = event.getAction();
     switch (action) {
      case MotionEvent.ACTION_DOWN:
       bx = event.getX();
       by = event.getY();
       break;
      case MotionEvent.ACTION_MOVE:
       canvas.drawLine(bx, by, event.getX(), event.getY(), paint);
       imageView.invalidate();
       bx = event.getX();
       by = event.getY();
       break;
      case MotionEvent.ACTION_UP:
       break;
      case MotionEvent.ACTION_CANCEL:
       break;
      default:
       break;
     }
     return true;
    }  
}



Tuesday, September 11, 2012

touch event in custom view (android)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        CustomView cv = new CustomView(this);
        cv.setBackgroundColor(Color.GRAY);
        
        setContentView(cv);
    }
}

class CustomView extends View
{  
 private Paint paint;
 private int positionX;
 private int positionY;
 private String l = "";
 
 public CustomView(Context context) {  
  super(context); 
  paint = new Paint();
  paint.setColor(Color.WHITE);
  paint.setTextSize(18);
  paint.setAntiAlias(true);
 }  
 protected void onDraw(Canvas canvas) {  
  // TODO Auto-generated method stub  
  super.onDraw(canvas);  
 
  canvas.drawText(l, 10, 100, paint);
  invalidate();
 }
 public boolean onTouchEvent(MotionEvent event)  
 {  
  positionX = (int)event.getRawX();  
  positionY = (int)event.getRawY();  
    
  switch(event.getAction())  
  {  
   case MotionEvent.ACTION_DOWN:  
    l = "DOWN - " + String.valueOf(positionX) + " " + String.valueOf(positionY); 
    break;  
      
   case MotionEvent.ACTION_MOVE:  
    l = "MOVE - " + String.valueOf(positionX) + " " + String.valueOf(positionY); 
    break;  

   case MotionEvent.ACTION_UP: 
    l = "UP - " + String.valueOf(positionX) + " " + String.valueOf(positionY); 
    break;   
  }
  return true;  
 }  
}  

Monday, September 10, 2012

Relative Layout + custom view (android)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        CustomView v = new CustomView(this);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100,100);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        //v.setLayoutParams(lp2);
        v.setBackgroundColor(Color.parseColor("#333333"));
        
        CustomView v2 = new CustomView(this);
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(100,100);
        lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        //v.setLayoutParams(lp2);
        v2.setBackgroundColor(Color.parseColor("#333333"));

        RelativeLayout relativeLayout = new RelativeLayout(this);
        RelativeLayout.LayoutParams _lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT);
        //relativeLayout.setLayoutParams(lp);
        relativeLayout.addView(v,lp);
        relativeLayout.addView(v2,lp2);
        
        setContentView(relativeLayout,_lp);
    }
}

class CustomView extends View
{  
 private Paint paint;
 
 public CustomView(Context context) {  
  super(context); 
  paint = new Paint();
  paint.setColor(Color.WHITE);
  paint.setTextSize(18);
  paint.setAntiAlias(true);
 }  
 protected void onDraw(Canvas canvas) {  
 // TODO Auto-generated method stub  
  super.onDraw(canvas);  
 
  canvas.drawText("Hello World", 5, 30, paint);
  invalidate();
 }
}  



Sunday, September 9, 2012

Monday, September 3, 2012

Simple Image Processing - Grayscale (C#)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * Created by SharpDevelop.
 * User: wai
 * Date: 2/9/2012
 * Time: 21:09
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.                                                       /
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

namespace PictureBox_Test
{
 /// <summary>
 /// Description of MainForm.
 /// </summary>
 public partial class MainForm : Form
 {
  PictureBox imageControl;
  
  public MainForm()
  {
   //
   // The InitializeComponent() call is required for Windows Forms designer support.
   //
   InitializeComponent();
   
   this.Size = new Size(400,300);
   
   imageControl = new PictureBox();
   imageControl.ImageLocation = "http://blog-imgs-31-origin.fc2.com/h/a/n/hanmans/pachinkotokyo18.jpg";
   imageControl.InitialImage = null;
   imageControl.Width = 400;
   imageControl.Height = 300;
   this.Controls.Add(imageControl);
   
   imageControl.LoadCompleted += PictureBox1_LoadCompleted;
  }
  private void PictureBox1_LoadCompleted(Object sender, AsyncCompletedEventArgs e) 
  {
   Bitmap image = new Bitmap(imageControl.Image);
   imageControl.Image = SetGrayscale(image);
  } 
  public Bitmap SetGrayscale(Bitmap image)
  {
   Bitmap bmap = image;
   Color c;
   for (int i = 0; i < bmap.Width; i++)
   {
    for (int j = 0; j < bmap.Height; j++)
    {
     c = bmap.GetPixel(i, j);
     byte gray = (byte)(.299 * c.R + .587 * c.G + .114 * c.B);

     bmap.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
    }
   }
   return bmap;
  }  
 }
}



PictureBox load image from url (C#)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * Created by SharpDevelop.
 * User: wai
 * Date: 2/9/2012
 * Time: 21:09
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.                                                       /
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace PictureBox_Test
{
 /// <summary>
 /// Description of MainForm.
 /// </summary>
 public partial class MainForm : Form
 {
  public MainForm()
  {
   //
   // The InitializeComponent() call is required for Windows Forms designer support.
   //
   InitializeComponent();
   
   this.Size = new Size(400,300);
   
   PictureBox imageControl = new PictureBox();
   imageControl.ImageLocation = "http://blog-imgs-31-origin.fc2.com/h/a/n/hanmans/pachinkotokyo18.jpg";
   imageControl.Width = 400;
   imageControl.Height = 300;
   this.Controls.Add(imageControl);
  }
 }
}



Sunday, September 2, 2012

Hello World 2 (C#)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * Created by SharpDevelop.
 * User: wai
 * Date: 2/9/2012
 * Time: 15:34
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace HelloWorld
{
 public partial class MainForm : Form
 {
  public MainForm()
  {
   InitializeComponent();
   
   this.Size = new Size(220, 150);
   
   Button button = new Button();
   button.Name = "Button01";
   button.Text = "Hi";
   button.Location = new Point(0,0);
   button.Size = new Size(100, 40);
   button.Click += new EventHandler(Click);
   this.Controls.Add(button);
  }
  
  private void Click(object sender, EventArgs e)
  {
     MessageBox.Show("Wow", "Message");
  }
 }
}



Hello World (C#)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
using System;

namespace HelloWorld
{
 class Program
 {
  public static void Main(string[] args)
  {
   Console.Write("Hello World !");
   Console.ReadKey(true);
  }
 }
}


Simple Vertical ScrollView (android)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class VerticalScrollViewActivity extends Activity {
 
    ImageView i;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        LinearLayout l = new LinearLayout(this);
        l.setOrientation(LinearLayout.VERTICAL);
        
        i = new ImageView(this);
        i.setImageResource(R.drawable.android);
        i.setPadding(5, 5, 0, 0);
        l.addView(i,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        i = new ImageView(this);
        i.setImageResource(R.drawable.android);
        i.setPadding(5, 5, 0, 0);
        l.addView(i,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        i = new ImageView(this);
        i.setImageResource(R.drawable.android);
        i.setPadding(5, 5, 0, 0);
        l.addView(i,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        i = new ImageView(this);
        i.setImageResource(R.drawable.android);
        i.setPadding(5, 5, 0, 0);
        l.addView(i,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        i = new ImageView(this);
        i.setImageResource(R.drawable.android);
        i.setPadding(5, 5, 0, 0);
        l.addView(i,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        i = new ImageView(this);
        i.setImageResource(R.drawable.android);
        i.setPadding(5, 5, 0, 0);
        l.addView(i,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        ScrollView m_Scroll = new ScrollView(this);
        m_Scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        m_Scroll.addView(l, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT) );
        m_Scroll.setOverScrollMode(1);
        m_Scroll.setSmoothScrollingEnabled(true);
        
        //addContentView(m_Scroll, new LayoutParams(LayoutParams.FILL_PARENT,
        //LayoutParams.WRAP_CONTENT));

        setContentView(m_Scroll);
    }
}



Google Map Test (android)