//[1] 获取屏幕分辨率
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
int width = manager.getDefaultDisplay().getWidth();
int height = manager.getDefaultDisplay().getHeight();
Log.d("screenscale",width+"------"+height);
//[2] 获取图片分辨率
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/dog.jpg",options);
int imgHeight = options.outHeight;
int imgWidth = options.outWidth;
Log.d("pictureInfo",imgWidth+"-----"+imgHeight);
//[3] 计算缩放比
int scale = 1;
int scaleX = imgWidth / width;
int scaleY = imgHeight / height;
if(scaleX >= scaleY && scaleX > scale)
scale = scaleX;
else if(scaleY > scaleX && scaleY > scale)
scale = scaleY;
Log.d("scale",scale+"");
//[4] 按照缩放比进行设置图片显示
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
Bitmap bitmap1 = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/dog.jpg",options);
//[5] 设置图片显示
iv.setImageBitmap(bitmap1);
//[2] 开始准备作画
//[2.1] 创建一个新的bitmap和原图配置一样
Bitmap copyBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());
//[2.2] 准备画笔
Paint paint = new Paint();
//[2.3] 准备一个画布,使用copyBitmap来进行相关的配置
Canvas canvas = new Canvas(copyBitmap);
//[2.4] 开始作画
canvas.drawBitmap(srcBitmap,new Matrix(),paint);
for(int x = 0;x < copyBitmap.getWidth();x++){ //添加一条黑线
copyBitmap.setPixel(x,100,Color.BLACK);
}
//[3] 将画展示到控件上
iv_copy.setImageBitmap(copyBitmap);
//[2.4] 图形旋转
//matrix.setRotate(30,5f,6f);
//[2.5] 图形缩放 前面的两个参数代表缩放的比例 后面的代表缩放的参考点
//matrix.setScale(0.5f,0.5f,srcBitmap.getWidth()/2,srcBitmap.getHeight()/2);
//[2.6] 图形平移
//matrix.setTranslate(30f,30f);
//[2.7] 图形的镜面效果
//matrix.setScale(-1.0f,1);
//matrix.postTranslate(srcBitmap.getWidth(),0);
//[2.8] 图形的倒影效果
//matrix.setScale(1.0f,-1.0f);
//matrix.postTranslate(0,srcBitmap.getHeight());
package com.drawboard;
import android.content.Intent;
import android.graphics.Bitmap;
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.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private ImageView iv;
private Paint paint;
private Bitmap copy;
private Canvas canvas;
private Bitmap src;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
//[1] 获取原背景
src = BitmapFactory.decodeResource(getResources(), R.drawable.background);
//[2] 进行背景模型
copy = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
//[3] 获取画笔
paint = new Paint();
//[4] 获取画布,铺上背景
canvas = new Canvas(copy);
//[5] 在画布上作画
canvas.drawBitmap(src, new Matrix(), paint);
//[6] 显示在控件上
iv.setImageBitmap(copy);
//[7] 设置iv的监听事件
iv.setOnTouchListener(new View.OnTouchListener() {
private float startX;
private float startY;
private float stopX;
private float stopY;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//[1] 获取事件类型
int action = motionEvent.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN: //当按下的时候触发
//[1] 获取触摸的坐标
startX = motionEvent.getX();
startY = motionEvent.getY();
break;
case MotionEvent.ACTION_MOVE: //当手指移动的时候触发
//[2] 获取移动中的坐标
stopX = motionEvent.getX();
stopY = motionEvent.getY();
//[3] 画线,从开始到结束
canvas.drawLine(startX,startY,stopX,stopY,paint);
//[3.1] 更新图片
iv.setImageBitmap(copy);
//[4] 更新原点
startX = stopX;
startY =stopY;
break;
case MotionEvent.ACTION_UP: //当手指松开的时候调用
break;
}
return true;
}
});
}
//点击按钮更改颜色
public void setColor(View view) {
paint.setColor(Color.BLUE);
}
//点击按钮加粗画笔
public void setStrokW(View view) {
paint.setStrokeWidth(10.0f);
}
//点击按钮保存图片 并且发送广播通知图库更新图片显示
public void save(View view) {
try {
boolean isSave = copy.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(Environment.getExternalStorageDirectory().getPath(), SystemClock.uptimeMillis()+".png")));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
sendBroadcast(intent); //4.4以后不能这么发广播,会报 Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
if(isSave){
Toast.makeText(getApplicationContext(),"保存成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"保存失败",Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void clear(View view){
copy = Bitmap.createBitmap(copy.getWidth(), copy.getHeight(), copy.getConfig());
canvas = new Canvas(copy);
canvas.drawBitmap(src,new Matrix(),paint);
iv.setImageBitmap(copy);
}
}
package com.mosatsu;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView)findViewById(R.id.iv);
Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.pre19);
final Bitmap copy = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
Paint paint = new Paint();
final Canvas canvas = new Canvas(copy);
Matrix matrix = new Matrix();
canvas.drawBitmap(src,matrix,paint);
iv.setImageBitmap(copy);
//[1] 给iv设置触摸事件
iv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
switch (action) {
case MotionEvent.ACTION_MOVE:
float x = motionEvent.getX();
float y = motionEvent.getY();
for (int i = -20; i < 20; i++) {
for(int j = -20;j < 20;j++){
if(Math.sqrt(i*i+j*j) < 20){ //为了达到圆形的效果,使用了勾股定理
try {
copy.setPixel((int) motionEvent.getX() + i, (int) motionEvent.getY() + j, Color.TRANSPARENT); //设置为透明
} catch (Exception e) {
}
}
}
}
iv.setImageBitmap(copy);
break;
}
return true;
}
});
}
}
//[1] 准备mediaplayer
player = new MediaPlayer();
//[2] 设置网络数据源
player.setDataSource("http://192.168.171.195:8080/dhc.mp3");
//[3] 进行异步准备,由底层C准备线程方法
player.prepareAsync();
//[4] 在准备完成后开始播放
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
player.start();
}
});
问题1:
05-20 18:35:24.305: E/MediaPlayer(19765): stop called in state 4
05-20 18:35:24.305: E/MediaPlayer(19765): error (-38, 0)
原因:是在调用prepareAsync()则以异步方式进入Prepared状态过程中即preparing状态中,调用了stop方法。
参考:http://stackoverflow.com/questions/8796956/mediaplayer-stop-called-in-state-4
问题2:
Media Player called in state 0, error (-38,0)
原因是在You're using prepareAsync, which is asynchronous. That is, you should wait for the onPrepared callback before you do anything that relies on the preparation to be complete (like calling start). That's why you get the "start called in state 4" error message (state 4 is MEDIA_PLAYER_PREPARING).
即调用 prepareAsync,在preparing过程中调用了start。
参考:http://stackoverflow.com/questions/16495276/mediaplayer-track-change-issue
问题3:
Attempt to call getDuration without a valid mediaplayer in media player on android
原因:在preparing过程中调用了getDuration方法。
You might be calling getDuration before the file is fully loaded.
问题4:如果你使用VideoView播放过MP4视频,你可能碰到过类似下面的问题:
MediaPlayer error (1, -2147483648) 如果你查阅文档,会发现1其实代表MEDIA_ERROR_UNKNOWN,不过文档对-2147483648(0x80000000)没有做什么说明,实际上它也是代表unknown error的意思。 真正的原因在于,MP4有多种编码格式,例如H.264,H.263等,而android版本较低的机器只支持部分编码。 一旦遭遇不被支持的编码格式,MediaPlayer可能就会抛出上面的错误信息。 如果你也遇到这类问题,你可以使用一些视频软件查看视频的编码格式,然后转换为普遍支持的格式。
vv = (VideoView)findViewById(R.id.vv);
vv.setVideoPath("http://192.168.171.195:8080/cc.3gp");
vv.start();