package com.gyarmy.paintpic; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.Log; 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.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; public class MainActivity extends Activity implements OnClickListener, OnSeekBarChangeListener, OnTouchListener { private static final String TAG = "菜单栏."; private View view_red; private View view_green; private View view_blue; private View view_yellow; private View view_pink; private SeekBar sb_bar; private ImageView iv; private Bitmap bitmap; private Canvas canvas; private Paint paint; float startX = 0; float startY = 0; int paintSize =1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); view_red = findViewById(R.id.view_red); view_green = findViewById(R.id.view_green); view_blue = findViewById(R.id.view_blue); view_yellow = findViewById(R.id.view_yellow); view_pink = findViewById(R.id.view_pink); sb_bar = (SeekBar) findViewById(R.id.skb_size); iv = (ImageView) findViewById(R.id.iv); view_red.setOnClickListener(this); view_green.setOnClickListener(this); view_blue.setOnClickListener(this); view_yellow.setOnClickListener(this); view_pink.setOnClickListener(this); sb_bar.setOnSeekBarChangeListener(this); iv.setOnTouchListener(this); //绘制图片 bitmap = Bitmap.createBitmap(480, 660, Config.ARGB_8888); canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); paint = new Paint(); } //选取颜色 @Override public void onClick(View v) { // TODO Auto-generated method stub if(v == view_red ){ paint.setColor(Color.RED); }else if(v == view_green){ paint.setColor(Color.GREEN); }else if(v == view_blue){ paint.setColor(Color.BLUE); }else if(v == view_yellow){ paint.setColor(Color.YELLOW); }else if(v == view_pink){ paint.setColor(Color.argb(255, 255, 80, 255)); } } //画笔粗细 @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub paintSize = progress; } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub paint.setStrokeWidth(paintSize); paint.setAntiAlias(true); } //绘画 @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX = event.getX(); startY = event.getY(); break; case MotionEvent.ACTION_MOVE: float endX = event.getX(); float endY = event.getY(); canvas.drawLine(startX, startY, endX, endY, paint); iv.setImageBitmap(bitmap); startX = endX; startY = endY; break; case MotionEvent.ACTION_UP: break; default: break; } //显示数据 return true; } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub //菜单 getMenuInflater().inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { // TODO Auto-generated method stub int itemId = item.getItemId(); switch (itemId) { case R.id.item_save: Log.d(TAG, " - 按钮保存了"); //保存图片 File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".jpg"); Log.d(TAG, Environment.getExternalStorageDirectory().toString()+System.currentTimeMillis()+".jpg"); FileOutputStream fos = null; try { fos = new FileOutputStream(file); bitmap.compress(CompressFormat.JPEG, 100, fos); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(fos!=null) { try { fos.close(); fos=null; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //广播 //异常 /* Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED); intent.setData(Uri.parse(Environment.getExternalStorageDirectory().toString())); sendBroadcast(intent); */ //无效 //sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(Environment.getExternalStorageDirectory()+""))); MediaScannerConnection.scanFile(this, new String[]{Environment.getExternalStorageDirectory()+""}, null, null); } break; default: break; } return super.onMenuItemSelected(featureId, item); } }
0则评论给“画图并保存”