Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Android use canvas board painting     - React Getting Started Tutorial (Linux)

- Merge sort Java implementation (Programming)

- By creating a personal technology blog Detailed GitHub (Linux)

- Atheros AR8161 / AR8162 network card driver problem solving in CentOS 6.4 (Linux)

- Linux Getting Started tutorial: XWindow what (Linux)

- Debian users to install FFmpeg 2.2.2 (Linux)

- CentOS 6.5 minimal installation and configuration VMware tools (Linux)

- LVM Disk Manager Application (Linux)

- Firewall Configuration Red Hat Enterprise Linux 4 (Linux)

- VNC connection VMware vSphere ESXi 5.5 (Linux)

- Linux Thread Synchronization (Programming)

- C language macro definition #define Usage (Programming)

- Nginx Proxy timeout Troubleshooting (Server)

- OpenGL Superb Learning Notes - Fragment Shader (Programming)

- Oracle data files deleted recover after physical (Database)

- Introduction to Linux system process monitoring tools (Linux)

- Oracle Linux 5.5 (64bit) Install Oracle 11gR2 RAC detailed tutorial (Database)

- CentOS 6.5 installation and deployment SVN 1.8.10 (Linux)

- To install Google Chrome browser under Ubuntu 14.04 LTS (Linux)

- Ubuntu install Tonido private cloud services (Server)

 
         
  Android use canvas board painting
     
  Add Date : 2017-08-31      
         
         
         
  Android use canvas painting board, first create a new project, create documents

First configure the page layout file activity_main.xml:

< LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
    xmlns: tools = "http://schemas.android.com/tools"
    android: layout_width = "match_parent"
    android: layout_height = "match_parent"
    android: orientation = "vertical">

    < ImageView
        android: id = "@ + id / iv"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: background = "@ drawable / bg"
        />
    < TextView
          android: layout_width = "wrap_content"
          android: layout_height = "wrap_content"
          android: text = "brush thickness"
        />
    < SeekBar
        android: id = "@ + id / sb"
        android: layout_width = "match_parent"
        android: layout_height = "wrap_content"
        android: max = "256"
        />
    < TextView
          android: layout_width = "wrap_content"
          android: layout_height = "wrap_content"
          android: text = "color"
        />
    < Spinner
        android: id = "@ + id / sp"
        android: layout_width = "wrap_content"
          android: layout_height = "wrap_content"
          android: entries = "@ array / color"
        />
    < Button
            android: id = "@ + id / btn"
          android: layout_width = "wrap_content"
          android: layout_height = "wrap_content"
          android: text = "Save"
        />
 
   

< / LinearLayout>

Then write the following code page code MainActivity.java

package com.xunfang.drawing;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.MonthDisplayHelper;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.Toast;


public class MainActivity extends Activity {
    private SeekBar sb;
    private ImageView iv;
    private Button btn;
    private Spinner sp;
    private String [] color;
    
    private Bitmap bm;
    private Bitmap copy;
    private Canvas canvas;
    private Paint paint;
    private File file;
    private int yanse;
        
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        
      // Get the array of colors defined in xml file
        color = getResources () getStringArray (R.array.color).;
        // Instantiate
        initData ();
        // Set up listeners
        setLister ();
        // Draw
        loadingImage ();
        
    }
    private void loadingImage () {
        // Load the original image
        bm = BitmapFactory.decodeResource (getResources (), R.drawable.bg);
        // Create a picture of the same size as the original blank image (a piece of paper, there is no data on it)
        copy = bm.createBitmap (bm.getWidth (), bm.getHeight (), bm.getConfig ());
        // Need a sketchpad, sketchpad spread on white paper
        canvas = new Canvas (copy);
        // Create a brush
        paint = new Paint ();
        
        // ImageView space to load a slide Listener
        iv.setOnTouchListener (new OnTouchListener () {
            int startx;
            int starty;
            @Override
            public boolean onTouch (View v, MotionEvent event) {
                // Get action
                int type = event.getAction ();
                switch (type) {
                case MotionEvent.ACTION_DOWN:
                    startx = (int) event.getX ();
                    starty = (int) event.getY ();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int endx = (int) event.getX ();
                    int endy = (int) event.getY ();
                    // Draw
                    canvas.drawLine (startx, starty, endx, endy, paint);
                    startx = (int) event.getX ();
                    starty = (int) event.getY ();
                    iv.setImageBitmap (copy);
                    break;
                case MotionEvent.ACTION_UP:
                    
                    break;
                }
                return true;
            }
        });
        
    }
    private void setLister () {
        // Drop-down box
        sp.setOnItemSelectedListener (new OnItemSelectedListener () {
            @Override
            public void onItemSelected (AdapterView < ?> parent, View view,
                    int position, long id) {
                Toast.makeText (getApplicationContext (), "you clicked on:" + color [position], 0) .show ();
                switch (position) {
                case 1:
                    paint.setColor (Color.GREEN);
                    break;
                case 2:
                    paint.setColor (Color.BLUE);
                    break;
                case 3:
                    paint.setColor (Color.BLACK);
                    break;
                case 4:
                    paint.setColor (Color.YELLOW);
                    break;
                case 0:
                    paint.setColor (Color.RED);
                    break;
                }
            }
            @Override
            public void onNothingSelected (AdapterView < ?> parent) {
            }
        });
        //save
        btn.setOnClickListener (new OnClickListener () {
            @Override
            public void onClick (View v) {
                try {
                    // Specify image storage path
                    file = new File (Environment.getExternalStorageDirectory () getAbsoluteFile () + "/ draw.png".);
                    FileOutputStream fos = new FileOutputStream (file);
                    copy.compress (CompressFormat.PNG, 100, fos);
                    Toast.makeText (getApplicationContext (), "saved successfully", 0) .show ();
                } Catch (Exception e) {
                }
                // Cheat the system, telling the system to insert a sd card
                Intent intent = new Intent ();
                intent.setAction (intent.ACTION_MEDIA_MOUNTED);
                intent.setData (Uri.fromFile (file));
                sendBroadcast (intent);
            }
        });
    }
    private void initData () {
            sb = (SeekBar) findViewById (R.id.sb);
            btn = (Button) findViewById (R.id.btn);
            sp = (Spinner) findViewById (R.id.sp);
            iv = (ImageView) findViewById (R.id.iv);
    }


}

AndroidManifest.xml configuration file as follows:

< ? Xml version = "1.0" encoding = "utf-8"?>
< Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
    package = "com.xunfang.drawing"
    android: versionCode = "1"
    android: versionName = "1.0">

    < Uses-sdk
        android: minSdkVersion = "16"
        android: targetSdkVersion = "21" />

    < Application
        android: allowBackup = "true"
        android: icon = "@ drawable / ic_launcher"
        android: label = "@ string / app_name"
        android: theme = "@ style / AppTheme">
        < Activity
            android: name = ". MainActivity"
            android: label = "@ string / app_name">
            < Intent-filter>
                < Action android: name = "android.intent.action.MAIN" />

                < Category android: name = "android.intent.category.LAUNCHER" />
            < / Intent-filter>
        < / Activity>
    < / Application>

< / Manifest>
     
         
         
         
  More:      
 
- Linux Kernel 4.2 Installation Instructions (Linux)
- Configuring xdebug debugging environment in Ubuntu 14.04 under the PhpStorm (Linux)
- AIX system file security aspects (Linux)
- The correct way to open Xcode - Debugging (Programming)
- CentOS7 installation GAMIT and GMT (Linux)
- Linux, ls command to achieve (Linux)
- Sorting algorithm of dichotomy (binary) insertion sort algorithm (Programming)
- Shell script to delete empty folders recursively (Linux)
- Ubuntu 15.04 installed JDK and configured as the default JDK (Linux)
- Configure shared library PCRE (Linux)
- Database start listening TNS-12537, TNS-12560 error (Database)
- How to run Docker client in Windows operating system (Linux)
- Access.log Apache access log analysis and how to import it into MySQL (Server)
- Ubuntu 14.04 build Android 5.1 development environment and compiler (Linux)
- CentOS achieve trash mechanism (Linux)
- System-level alias vs Oracle ADR functionality (Database)
- How linux network security configuration (Linux)
- Linux shell script to adjust the Task Scheduler (Linux)
- Linux, modify / etc / profile file (Linux)
- Java Foundation - implicit conversion vs cast (Programming)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.