随手涂鸦

来源:互联网 发布:情义我心知 廖伟雄 编辑:程序博客网 时间:2024/05/18 12:30

案例: 随手涂鸦

效果展示


项目目录结构


项目代码展示

在src的Java代码 com.itheima.painter.MainActivity

    import java.io.File;    import java.io.FileOutputStream;    import android.app.Activity;    import android.content.Intent;    import android.graphics.Bitmap;    import android.graphics.Bitmap.CompressFormat;    import android.graphics.Canvas;    import android.graphics.Color;    import android.graphics.Paint;    import android.net.Uri;    import android.os.Bundle;    import android.os.Environment;    import android.os.SystemClock;    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.ImageView;    import android.widget.SeekBar;    import android.widget.SeekBar.OnSeekBarChangeListener;    import android.widget.Toast;    public class MainActivity extends Activity implements OnClickListener {        private View red, green, yellow, purple, blue;        private SeekBar seekbar;        private Paint paint;        private ImageView iv;        /**         * 一个可以被修改的图片         */        private Bitmap alterBitmap;        /**         * 画板         */        private Canvas canvas;        @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main);            // 创建一个空白的图片            alterBitmap = Bitmap.createBitmap(320, 320, Bitmap.Config.ARGB_8888);            canvas = new Canvas(alterBitmap);            paint = new Paint();            // 设置画笔的颜色            paint.setColor(Color.BLACK);            canvas.drawColor(Color.WHITE);            // 设置画笔的宽度            paint.setStrokeWidth(5);            seekbar = (SeekBar) findViewById(R.id.seekbar);            seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {                @Override                public void onStopTrackingTouch(SeekBar seekBar) {                    int progress = seekBar.getProgress();                    paint.setStrokeWidth(progress);                    Toast.makeText(MainActivity.this, "画笔宽度为:" + progress, 0)                            .show();                }                @Override                public void onStartTrackingTouch(SeekBar seekBar) {                }                @Override                public void onProgressChanged(SeekBar seekBar, int progress,                        boolean fromUser) {                }            });            red = findViewById(R.id.red);            red.setOnClickListener(this);            green = findViewById(R.id.green);            green.setOnClickListener(this);            yellow = findViewById(R.id.yellow);            yellow.setOnClickListener(this);            purple = findViewById(R.id.purple);            purple.setOnClickListener(this);            blue = findViewById(R.id.blue);            blue.setOnClickListener(this);            iv = (ImageView) findViewById(R.id.iv);            // 给imageview的控件注册一个触摸事件            iv.setOnTouchListener(new OnTouchListener() {                int startX;                int startY;                // 当imageview被触摸的时候调用的方法.                @Override                public boolean onTouch(View v, MotionEvent event) {                    switch (event.getAction()) {                    case MotionEvent.ACTION_DOWN:// 按下                        System.out.println("摸到");                        startX = (int) event.getX();                        startY = (int) event.getY();                        break;                    case MotionEvent.ACTION_MOVE:// 移动                        System.out.println("移动");                        int newX = (int) event.getX();                        int newY = (int) event.getY();                        canvas.drawLine(startX, startY, newX, newY, paint);                        iv.setImageBitmap(alterBitmap);                        // 记得重新初始化手指在屏幕上的坐标                        startX = (int) event.getX();                        startY = (int) event.getY();                        break;                    case MotionEvent.ACTION_UP:// 离开                        System.out.println("放手");                        break;                    }                    return true;// false代表的是事件没有处理完毕,等待事件处理完毕, true代表事件已经处理完毕了.                }            });        }        @Override        public void onClick(View v) {            switch (v.getId()) {            case R.id.red:                paint.setColor(Color.RED);                Toast.makeText(MainActivity.this, "画笔红色", 0).show();                break;            case R.id.yellow:                paint.setColor(Color.YELLOW);                Toast.makeText(MainActivity.this, "画笔黄色", 0).show();                break;            case R.id.green:                paint.setColor(Color.GREEN);                Toast.makeText(MainActivity.this, "画笔绿色", 0).show();                break;            case R.id.purple:                paint.setColor(0xffff00ff);                Toast.makeText(MainActivity.this, "画笔紫色", 0).show();                break;            case R.id.blue:                paint.setColor(Color.BLUE);                Toast.makeText(MainActivity.this, "画笔蓝色", 0).show();                break;            }        }        @Override        public boolean onCreateOptionsMenu(Menu menu) {            getMenuInflater().inflate(R.menu.main, menu);            return true;        }        @Override        public boolean onOptionsItemSelected(MenuItem item) {            if (item.getItemId() == R.id.item_save) {                // 保存图片                try {                    File file = new File(Environment.getExternalStorageDirectory(),                            SystemClock.currentThreadTimeMillis() + ".jpg");                    FileOutputStream stream = new FileOutputStream(file);                    alterBitmap.compress(CompressFormat.JPEG, 100, stream);                    stream.close();                    Toast.makeText(this, "保存成功", 0).show();                    //模拟一个sd卡插入广播.                    Intent intent = new Intent();                    intent.setAction(Intent.ACTION_MEDIA_MOUNTED);                    intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));                    sendBroadcast(intent);                } catch (Exception e) {                    e.printStackTrace();                    Toast.makeText(this, "保存失败", 0).show();                }            }            return super.onOptionsItemSelected(item);        }    }

在res/layout/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"        tools:context=".MainActivity" >        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="请选择画笔的颜色" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal" >            <View                android:id="@+id/red"                android:layout_width="35dip"                android:layout_height="35dip"                android:background="#ff0000" />            <View                android:id="@+id/green"                android:layout_width="35dip"                android:layout_height="35dip"                android:background="#00ff00" />            <View                android:id="@+id/blue"                android:layout_width="35dip"                android:layout_height="35dip"                android:background="#0000ff" />            <View                android:id="@+id/yellow"                android:layout_width="35dip"                android:layout_height="35dip"                android:background="#ffff00" />            <View                android:id="@+id/purple"                android:layout_width="35dip"                android:layout_height="35dip"                android:background="#ff00ff" />        </LinearLayout>        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="请设置画笔的粗细" />        <SeekBar            android:id="@+id/seekbar"            android:max="20"            android:progress="5"            android:layout_width="match_parent"            android:layout_height="wrap_content" />        <ImageView            android:id="@+id/iv"            android:layout_width="320dip"            android:layout_height="320dip" />    </LinearLayout>

在安卓的清单文件 AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>    <manifest xmlns:android="http://schemas.android.com/apk/res/android"        package="com.itheima.painter"        android:versionCode="1"        android:versionName="1.0" >        <uses-sdk            android:minSdkVersion="8"            android:targetSdkVersion="17" />        <!-- 写入SDcard的权限 -->        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />        <application            android:allowBackup="true"            android:icon="@drawable/ic_launcher"            android:label="@string/app_name"            android:theme="@style/AppTheme" >            <activity                android:name="com.itheima.painter.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>
0 0
原创粉丝点击