标签:
创建对话框构建器对象,类似工厂模式
AlertDialog.Builder builder = new Builder(this);
设置标题和正文
builder.setTitle("警告");
builder.setMessage("若练此功,必先自宫");
设置确定和取消按钮
builder.setPositiveButton("现在自宫", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "恭喜你自宫成功,现在程序退出", 0).show();
}
});
builder.setNegativeButton("下次再说", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "若不自宫,一定不成功", 0).show();
}
});
使用构建器创建出对话框对象
AlertDialog ad = builder.create();
ad.show();
创建对话框对象
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("选择你的性别");
定义单选选项
final String[] items = new String[]{
"男", "女", "其他"
};
// -1表示默认选择
builder.setSingleChoiceItems(items, -1, new OnClickListener() {
// which表示点击的是哪一个选项
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您选择了" + items[which], 0).show();
// 对话框消失
dialog.dismiss();
}
});
builder.show();
定义多选的选项,因为可以多选,所以需要一个boolean数组来记录哪些选项被选了
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("请选择你认为最帅的人");
final String[] items = new String[]{
"赵帅哥",
"赵师哥",
"赵老师",
"侃哥"
};
// true表示对应位置的选项被选了
final boolean[] checkedItems = new boolean[]{
true,
false,
false,
false,
};
builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
// 点击某个选项,如果该选项之前没被选择,那么此时isChecked的值为true
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
}
});
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuffer sb = new StringBuffer();
for(int i = 0;i < items.length; i++){
sb.append(checkedItems[i] ? items[i] + " " : "");
}
Toast.makeText(MainActivity.this, sb.toString(), 0).show();
}
});
builder.show();
获取屏幕宽高
Display dp = getWindowManager().getDefaultDisplay();
int screenWidth = dp.getWidth();
int screenHeight = dp.getHeight();
获取图片宽高
Options opts = new Options();
// 请求图片属性但不申请内存
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile("sdcard/dog.jpg", opts);
int imageWidth = opts.outWidth;
int imageHeight = opts.outHeight;
图片的宽高除以屏幕宽高,算出宽和高的缩放比例,取较大值作为图片的缩放比例
int scale = 1;
int scaleX = imageWidth / screenWidth;
int scaleY = imageHeight / screenHeight;
if(scaleX >= scaleY && scaleX > 1){
scale = scaleX;
}
else if(scaleY > scaleX && scaleY > 1){
scale = scaleY;
}
按缩放比例加载图片
// 设置缩放比例
opts.inSampleSize = scale;
// 为图片申请内存
opts.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile("sdcard/dog.jpg", opts);
iv.setImageBitmap(bm);
直接加载的bitmap对象是只读的,无法修改,要修改图片只能在内存中创建出一个一模一样的bitmap副本,然后修改副本
// 加载原图
Bitmap srcBm = BitmapFactory.decodeFile("sdcard/photo3.jpg");
iv_src.setImageBitmap(srcBm);
// 创建与原图大小一致的空白bitmap
Bitmap copyBm = Bitmap.createBitmap(srcBm.getWidth(), srcBm.getHeight(), srcBm.getConfig());
// 定义画笔
Paint paint = new Paint();
// 把纸铺在画版上
Canvas canvas = new Canvas(copyBm);
// 把srcBm的内容绘制在copyBm上
canvas.drawBitmap(srcBm, new Matrix(), paint);
iv_copy.setImageBitmap(copyBm);
首先定义一个矩阵对象
Matrix mt = new Matrix();
缩放效果
// x轴缩放1倍,y轴缩放0.5倍
mt.setScale(1, 0.5f);
旋转效果
// 以图片宽高的一半中心点为轴点,顺时旋转30度
mt.setRotate(30, copyBm.getWidth() / 2, copyBm.getHeight() / 2);
平移
// x轴坐标+10,y轴坐标+20
mt.setTranslate(10, 20);
镜面
// 把X坐标都变成负数
mt.setScale(-1, 1);
// 图片整体向右移
mt.postTranslate(copyBm.getWidth(), 0);
倒影
// 把Y坐标都变成负数
mt.setScale(1, -1);
// 图片整体向下移
mt.postTranslate(0, copyBm.getHeight());
功能:记录用户触摸事件的XY坐标,绘制直线
给ImageView设置触摸侦听,得到用户的触摸事件,并获知用户触摸ImageView的坐标
iv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
// 触摸屏幕
case MotionEvent.ACTION_DOWN:
// 得到触摸屏幕时手指的坐标
startX = (int) event.getX();
startY = (int) event.getY();
break;
// 在屏幕上滑动
case MotionEvent.ACTION_MOVE:
// 用户滑动手指,坐标不断的改变,获取最新坐标
int newX = (int) event.getX();
int newY = (int) event.getY();
// 用上次onTouch方法得到的坐标和本次得到的坐标绘制直线
canvas.drawLine(startX, startY, newX, newY, paint);
iv.setImageBitmap(copyBm);
startX = newX;
startY = newY;
break;
}
return true;
}
});
刷子效果,加粗画笔
paint.setStrokeWidth(8);
调色板,改变画笔颜色
paint.setColor(Color.GREEN);
保存图片至SD卡
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("sdcard/dazuo.png"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 保存图片
copyBm.compress(CompressFormat.PNG, 100, fos);
系统开机或者点击加载sd卡按钮时,系统会发送sd卡就绪广播,我们也可以手动发送就绪广播
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
sendBroadcast(intent);
原理:把穿内衣和穿外衣的照片重叠显示,内衣照在下面,用户滑动屏幕时,触摸的是外衣照,把手指经过的像素都置为透明,内衣照就显示出来了
给屏幕设置移动监听,把指定的像素设置为透明
iv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
int newX = (int) event.getX();
int newY = (int) event.getY();
// 把指定的像素变成透明
copyBm.setPixel(newX, newY, Color.TRANSPARENT);
iv.setImageBitmap(copyBm);
break;
}
return true;
}
});
每次只设置一个像素点太慢,以触摸的像素为圆心,半径为5画圆,圆内的像素全部置为透明
for (int i = -5; i < 6; i++) {
for (int j = -5; j < 6; j++) {
if(Math.sqrt(i * i + j * j) <= 5)
copyBm.setPixel(newX + i, newY + j, Color.TRANSPARENT);
}
}
服务里定义play、stop、pause、continuePlay等方法
// 播放
private void play() {
player.reset();
try {
player.setDataSource("sdcard/bzj.mp3");
player.prepare();
} catch (Exception e) {
e.printStackTrace();
}
player.start();
}
// 暂停
private void pause() {
player.pause();
}
// 停止播放
private void stop() {
player.stop();
}
// 继续播放
private void continuePlay() {
player.start();
}
先start启动MusicService,再bind
Intent intent = new Intent(this, MusicService.class);
startService(intent);
bindService(intent, conn, BIND_AUTO_CREATE);
获取当前的播放时间和当前音频的最长时间
int currentPosition = player.getCurrentPosition();
int duration = player.getDuration();
发消息至Handler,把播放进度放进Message对象中,在Handler中更新SeekBar的进度
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
int currentPosition = player.getCurrentPosition();
int duration = player.getDuration();
Message msg = Message.obtain();
// 把播放进度存入Message中
Bundle data = new Bundle();
data.putInt("currentPosition", currentPosition);
data.putInt("duration", duration);
msg.setData(data);
MainActivity.handler.sendMessage(msg);
}
}, 5, 500);
在Activity中定义Handler
static Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
// 取出消息携带的数据
Bundle data = msg.getData();
int currentPosition = data.getInt("currentPosition");
int duration = data.getInt("duration");
// 设置播放进度
sb.setMax(duration);
sb.setProgress(currentPosition);
};
};
设置拖动监听,拖动后把进度条设置到拖动停止的位置
// 给sb设置一个拖动侦听
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
//停止拖动时调用
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
mi.seekTo(progress);
}
// 开始拖动时调用
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
// 拖动的时候不断调用
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
});
播放视频也是用MediaPlayer,不过跟音频不同,要设置显示在哪个SurfaceView
SurfaceView sv = (SurfaceView) findViewById(R.id.sv);
SurfaceHolder sh = sv.getHolder();
MediaPlayer player = new MediaPlayer();
player.reset();
try {
player.setDataSource("sdcard/2.3gp");
player.setDisplay(sh);
player.prepare();
} catch (Exception e) {
e.printStackTrace();
}
player.start();
给SurfaceHolder设置CallBack,类似于侦听,可以知道SurfaceView的状态
sh.addCallback(new Callback() {
// SurfaceView销毁时调用
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
// SurfaceView创建时调用
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
});
启动系统提供的拍照程序
// 隐式启动系统提供的拍照Activity
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 设置照片的保存路径
File file = new File(Environment.getExternalStorageDirectory(), "haha.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, 0);
启动系统提供的摄像程序
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "haha.3gp");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
// 设置保存视频文件的质量
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, 0);
标签:
原文地址:http://blog.csdn.net/u013443865/article/details/51457544