标签:android style blog http java color
主页面布局文件很简单就是一个按钮弹出对话框,一个imageView显示选择的图片。
我给大家提供了下载代码在这个网页:http://download.csdn.net/download/u011348453/7669871
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/mybut"
android:text="用户登录"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/img_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
弹出对话框是两个按钮,一个是拍照,一个是从相册中提取照片。
photo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageButton
android:id="@+id/img_takephoto"
android:layout_marginLeft="15dip"
android:layout_marginTop="20dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/a_takephoto"
/>
<ImageView
android:layout_margin="10dip"
android:layout_width="240dip"
android:layout_height="2dip"
android:background="#CACDCF"
/>
<ImageButton
android:id="@+id/img_getphoto"
android:layout_marginLeft="15dip"
android:layout_marginBottom="20dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/a_localphoto"
/>
</LinearLayout>
先给出主要的代码
package org.lxh.demo;
import java.io.ByteArrayOutputStream;
import java.io.File;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MyDialogDemo extends Activity {
private Button mybut = null; // 定义按钮
private ImageButton takephoto;
private ImageButton getphoto;
private static final int NONE = 0;
private static final int PHOTO_GRAPH = 1;// 拍照
private static final int PHOTO_ZOOM = 2; // 缩放
private static final int PHOTO_RESOULT = 3;// 结果
private static final String IMAGE_UNSPECIFIED = "image/*";
private ImageView imageView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.mybut = (Button) super.findViewById(R.id.mybut); // 取得按钮
this.mybut.setOnClickListener(new OnClickListenerImpl()); // 设置事件类
imageView = (ImageView) findViewById(R.id.img_photo);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == NONE)
return;
// 拍照
if (requestCode == PHOTO_GRAPH) {
// 设置文件保存路径
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
startPhotoZoom(Uri.fromFile(picture));
}
if (data == null)
return;
// 读取相册缩放图片
if (requestCode == PHOTO_ZOOM) {
startPhotoZoom(data.getData());
}
// 处理结果
if (requestCode == PHOTO_RESOULT) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0-100)压缩文件
imageView.setImageBitmap(photo); //把图片显示在ImageView控件上
}
}
super.onActivityResult(requestCode, resultCode, data);
}
//------------------------------------------------------------------------1
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
LayoutInflater factory = LayoutInflater.from(MyDialogDemo.this);
View myView = factory.inflate(R.layout.a_login, null);
// final Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this)
// .setView(myView).create();
// dialog.show();
final Dialog dialog = new Dialog(MyDialogDemo.this, R.style.dialog);
dialog.setContentView(myView);
dialog.show();
takephoto = (ImageButton) myView.findViewById(R.id.img_takephoto);
getphoto = (ImageButton) myView.findViewById(R.id.img_getphoto);
// 从拍照获取图片
takephoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("MyDialogDemo", "sslfjsljls");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(
MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "temp.jpg")));
dialog.dismiss();
startActivityForResult(intent, PHOTO_GRAPH);
}
});
// 从相册获取图片
getphoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
IMAGE_UNSPECIFIED);
dialog.dismiss();
startActivityForResult(intent, PHOTO_ZOOM);
}
});
}
}
//------------------------------------------------------------------2
/**
* 收缩图片
*
* @param uri
*/
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 500);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTO_RESOULT);
}
}
点击模块1
//------------------------------------------------------------------------1
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
LayoutInflater factory = LayoutInflater.from(MyDialogDemo.this);
View myView = factory.inflate(R.layout.a_login, null);
// final Dialog dialog = new AlertDialog.Builder(MyDialogDemo.this)
// .setView(myView).create();
// dialog.show();
final Dialog dialog = new Dialog(MyDialogDemo.this, R.style.dialog);
dialog.setContentView(myView);
dialog.show();
takephoto = (ImageButton) myView.findViewById(R.id.img_takephoto);
getphoto = (ImageButton) myView.findViewById(R.id.img_getphoto);
// 从拍照获取图片
takephoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("MyDialogDemo", "sslfjsljls");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(
MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "temp.jpg")));
dialog.dismiss();
startActivityForResult(intent, PHOTO_GRAPH);
}
});
// 从相册获取图片
getphoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
IMAGE_UNSPECIFIED);
dialog.dismiss();
startActivityForResult(intent, PHOTO_ZOOM);
}
});
}
}
这里设置对话框样式,去掉对话框边框
final Dialog dialog = new Dialog(MyDialogDemo.this, R.style.dialog);
dialog.setContentView(myView);
dialog.show();
里面的R.style.dialog自定义在style里
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/black</item>
<item name="android:windowBackground">@null</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
回调方法模块2
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == NONE)
return;
// 拍照
if (requestCode == PHOTO_GRAPH) {
// 设置文件保存路径
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
startPhotoZoom(Uri.fromFile(picture));
}
if (data == null)
return;
// 读取相册缩放图片
if (requestCode == PHOTO_ZOOM) {
startPhotoZoom(data.getData());
}
// 处理结果
if (requestCode == PHOTO_RESOULT) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0-100)压缩文件
//此处可以把Bitmap保存到sd卡中,具体请看:http://www.cnblogs.com/linjiqin/archive/2011/12/28/2304940.html
imageView.setImageBitmap(photo); //把图片显示在ImageView控件上
}
}
super.onActivityResult(requestCode, resultCode, data);
}
收缩图片模块3(方便手机显示小图标)
/**
* 收缩图片
*
* @param uri
*/
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 500);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTO_RESOULT);
}
拍照保存(调用手机),对话框的自定义样式,布布扣,bubuko.com
标签:android style blog http java color
原文地址:http://www.cnblogs.com/bxfx111/p/3863799.html