【黑马Android】(10)绑定的方式调用服务的方法/图片的各种操作/人脸识别
来源:互联网 发布:apk淘宝哪个类目 编辑:程序博客网 时间:2024/06/02 02:34
绑定的方式调用服务的方法
服务的生命周期:
一、采用start的方式开启服务
生命周期如下:
onStart()过时了
开启服务:onCreate()--> onStartCommand() ---> onDestory();
如果服务已经开启,不会重复的执行onCreate(), 而是会调用onStart()和 onStartCommand();
服务停止的时候onDestory().
服务只会被停止一次
二、服务还有一种开启方式,绑定的方式开启服务。
onCreate()--->onBind();--->onunbind()-->onDestory();
绑定服务不会调用onstart或者onstartcommand方法;
混合调用的服务的生命周期:
服务长期后台运行,又想调用服务的方法:
1.start方式开启服务(保证服务长期后台运行)
2.bind方式绑定服务(保证调用服务的方法)
3.unbind解除绑定服务
4.stopService停止服务。
三、两种开启服务方法的区别。
start方式开启服务。一旦服务开启跟调用者(开启者)就没有任何关系了。
开启者退出了,开启者挂了,服务还在后台长期的运行。
开启者没有办法去调用服务里面的方法。(美国的司法独立)
bind的方式开启服务,绑定服务,调用者挂了,服务也会跟着挂掉。不求同时生,但求同时死。
开启者可以调用服务里面的方法。服务的生命周期
<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" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="start" android:text="开启服务" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="stop" android:text="停止服务" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="call" android:text="调用服务里面的方法" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.servicelife" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.servicelife.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> <service android:name="com.itheima.servicelife.MyService"></service> </application></manifest>
package com.itheima.servicelife;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void start(View view){ Intent intent = new Intent(this,MyService.class); //通知框架开启服务。 startService(intent); } public void stop(View view){ Intent intent = new Intent(this,MyService.class); stopService(intent); } @Override protected void onDestroy() { System.out.println("啊啊啊,我是activity,我挂了"); super.onDestroy(); } //调用服务里面的方法。不可以自己new服务,调用的服务的方法,必须通过框架得到服务的引用。 public void call(View view){ }}
package com.itheima.servicelife;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.widget.Toast;public class MyService extends Service {@Overridepublic IBinder onBind(Intent arg0) {return null;}@Overridepublic void onCreate() {System.out.println("oncreate");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("onstartcommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {System.out.println("ondestory");super.onDestroy();}/** * 这是服务里面的一个方法 */public void methodInService(){Toast.makeText(this, "哈哈,我是服务里面的方法", 0).show();}}
绑定服务
<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" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="bind" android:text="绑定服务" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="unbind" android:text="解除绑定服务" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="call" android:text="调用服务里面的方法" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.servicelife" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.servicelife.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> <service android:name="com.itheima.servicelife.MyService"></service> </application></manifest>
package com.itheima.servicelife;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;public class MainActivity extends Activity {private MyConn conn ;private IMiddlePerson mp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //绑定服务 public void bind(View view){ //3.activity采用绑定的方式去开启服务。 Intent intent = new Intent(this,MyService.class); conn = new MyConn(); bindService(intent, conn, BIND_AUTO_CREATE); } //解除绑定服务 public void unbind(View view){ unbindService(conn); } @Override protected void onDestroy() { System.out.println("啊啊啊,我是activity,我挂了"); super.onDestroy(); } //调用服务里面的方法。 public void call(View view){ //5.通过中间人调用服务里面的方法。 mp.callMethodInService(55); } private class MyConn implements ServiceConnection{ //4. 当服务被连接的时候调用 服务别成功 绑定的时候调用@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {System.out.println("在activity里面成功得到了中间人");mp = (IMiddlePerson) service;}//当服务失去连接的时候调用(一般进程挂了,服务被异常杀死)@Overridepublic void onServiceDisconnected(ComponentName name) {} }}
采用接口抽取方法
package com.itheima.servicelife;/** * 中间人的接口定义 * */public interface IMiddlePerson {/** * 代办暂住证 * @param money */public void callMethodInService(int money);}
package com.itheima.servicelife;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.widget.Toast;public class MyService extends Service {//2.实现服务成功绑定的代码 ,返回一个中间人。@Overridepublic IBinder onBind(Intent arg0) {System.out.println("服务被成功绑定了。。。。");return new MiddlePerson();}@Overridepublic boolean onUnbind(Intent intent) {System.out.println("onunbind");return super.onUnbind(intent);}@Overridepublic void onCreate() {System.out.println("oncreate");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("onstartcommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {System.out.println("ondestory");super.onDestroy();}/** * 这是服务里面的一个方法 */public void methodInService(){Toast.makeText(this, "哈哈,服务给你办好了暂住证。", 0).show();}//1.第一步服务要暴露方法 必须要有一个中间人private class MiddlePerson extends Binder implements IMiddlePerson{/** * 代办暂住证 * @param money 给钱 50块钱以上才给办。 */public void callMethodInService(int money){if(money>=50){methodInService();}else{Toast.makeText(getApplicationContext(), "多准备点钱。", 0).show();}}/** * 陪领导打麻将 */public void playMajiang(){System.out.println("陪领导打麻将。");}}}
aidl绑定远程服务
远程服务
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.remoteservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.remoteservice.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> <service android:name="com.itheima.remoteservice.RemoteService"> <intent-filter > <action android:name="com.itheima.remoteservice"/> </intent-filter> </service> </application></manifest>
package com.itheima.remoteservice;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}
package com.itheima.remoteservice;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class RemoteService extends Service {@Overridepublic void onCreate() {System.out.println("远程服务被创建了。。。");super.onCreate();}@Overridepublic void onDestroy() {System.out.println("远程服务被销毁了。");super.onDestroy();}@Overridepublic IBinder onBind(Intent intent) {return new MiddlePerson();}private void methodInService(){System.out.println("我是远程服务的方法,我被调用了。。。。");}//1.创建一个中间人 远程服务继承的是ipc的一个实现类private class MiddlePerson extends IMiddlePerson.Stub{@Overridepublic void callMethodInService() {methodInService();}}}
IMiddlePerson.aidl
package com.itheima.remoteservice; interface IMiddlePerson {/** * 调用服务里面的方法 */ void callMethodInService();}
绑定远程服务
<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" > <Button android:onClick="bind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="绑定远程服务" /> <Button android:onClick="unbind" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="解除绑定远程服务" /> <Button android:onClick="call" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="调用远程服务的方法" /></LinearLayout>
IMiddlePerson.aidl
package com.itheima.remoteservice; interface IMiddlePerson {/** * 调用服务里面的方法 */ void callMethodInService();}
package com.itheima.bindremote;import com.itheima.remoteservice.IMiddlePerson;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.view.View;public class MainActivity extends Activity {private MyConn conn;private IMiddlePerson iMp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}/** * 绑定远程服务 * @param view */public void bind(View view){Intent intent = new Intent();intent.setAction("com.itheima.remoteservice");conn = new MyConn();bindService(intent, conn, BIND_AUTO_CREATE);}private class MyConn implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {iMp = IMiddlePerson.Stub.asInterface(service);} @Overridepublic void onServiceDisconnected(ComponentName name) {}}public void call(View view){try {iMp.callMethodInService();} catch (RemoteException e) {e.printStackTrace();}}@Overrideprotected void onDestroy() {unbindService(conn);super.onDestroy();}}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.bindremote" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.bindremote.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>
远程服务:调用者和服务在不同的工程代码里面。
本地服务:调用者和服务在同一个工程代码里面。
每一个应用程序都是运行在自己独立的进程里面的。
进程操作系统分配内存空间的一个单位。进程的数据都是独立的。独立的内存空间。
aidl:android interface definition language 安卓接口定义语言
aidl文件都是公有的,没有访问权限修饰符
IPC: inter process communication进程间通讯
绑定本地服务调用方法的步骤:
1.在服务的内部创建一个内部类 提供一个方法,可以间接调用服务的方法
private class MiddlePerson extends Binder implements IMiddlePerson{}
2.实现服务的onbind方法,返回的就是中间人MiddlePerson
3.在activity绑定服务。bindService();
4.在服务成功绑定的时候 会执行一个方法 onServiceConnected传递过来一个 IBinder对象
5.强制类型转化 调用接口里面的方法。
绑定远程服务调用方法的步骤:
1.在服务的内部创建一个内部类 提供一个方法,可以间接调用服务的方法
2.把暴露的接口文件的扩展名改为aidl文件 去掉访问修饰符public
private class MiddlePerson extends IMiddlePerson.Stub{} IPC的子类
3.实现服务的onbind方法,返回的就是中间人IMiddlePerson
4.在activity绑定服务。bindService();
5.在服务成功绑定的时候 会执行一个方法 onServiceConnected传递过来一个 IBinder对象
6.IMiddlePerson.Stub.asInterface(binder) 调用接口里面的方法。
绑定服务的应用场景
getSystemService();
混合调用服务的案例
支付宝
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.alipay" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.alipay.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> <service android:name="com.itheima.alipay.SafePayService"> <intent-filter > <action android:name="com.itheima.alipay"/> </intent-filter> </service> </application></manifest>
package com.itheima.alipay;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}
package com.itheima.alipay;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.os.RemoteException;public class SafePayService extends Service {@Overridepublic IBinder onBind(Intent intent) {System.out.println("服务被绑定 onbind");return new MyBinder();}/** * 安全支付的方法 */private boolean pay(long time,String pwd,double money){if("123".equals(pwd)){return true;}else{return false;}}private class MyBinder extends ISafePay.Stub{/** * 调用安全支付的逻辑 */@Overridepublic boolean callPay(long time, String pwd, double money)throws RemoteException {return pay(time, pwd, money);}}@Overridepublic void onCreate() {System.out.println("oncreate支付宝服务被创建,一直在后台运行,检查手机的安全状态");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("服务onstart");return super.onStartCommand(intent, flags, startId);}@Overridepublic boolean onUnbind(Intent intent) {System.out.println("onunbind");return super.onUnbind(intent);}@Overridepublic void onDestroy() {System.out.println("ondestory支付宝服务被销毁");super.onDestroy();}}
package com.itheima.alipay;interface ISafePay{boolean callPay(long time,String pwd,double money);}
捕鱼达人
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.fish" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.fish.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>
<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" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="click" android:text="买5个炮弹" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="start" android:text="开启服务" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="stop" android:text="停止服务" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="bind" android:text="绑定服务" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="unbind" android:text="解除绑定服务" /></LinearLayout>
package com.itheima.alipay;interface ISafePay{boolean callPay(long time,String pwd,double money);}
package com.itheima.fish;import com.itheima.alipay.ISafePay;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {private ISafePay iSafePay;private MyConn conn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//Intent intent = new Intent();//intent.setAction("com.itheima.alipay");//startService(intent);//保证服务长期后台运行。}public void start(View view){Intent intent = new Intent();intent.setAction("com.itheima.alipay");startService(intent);}public void stop(View view){Intent intent = new Intent();intent.setAction("com.itheima.alipay");stopService(intent);}public void bind(View view){Intent intent = new Intent();intent.setAction("com.itheima.alipay");conn = new MyConn();bindService(intent, conn, BIND_AUTO_CREATE);//异步的操作}public void unbind(View view){unbindService(conn);}public void click(View view){Intent intent = new Intent();intent.setAction("com.itheima.alipay");conn = new MyConn();bindService(intent, conn, BIND_AUTO_CREATE);//异步的操作//绑定服务调用服务的方法。}private class MyConn implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {iSafePay = ISafePay.Stub.asInterface(service);try {boolean result = iSafePay.callPay(System.currentTimeMillis(), "123", 3.52f);if(result){Toast.makeText(getApplicationContext(), "支付成功,获取大炮弹", 0).show();}else{Toast.makeText(getApplicationContext(), "支付失败,请重试", 0).show();}//unbindService(conn);//conn = null;} catch (RemoteException e) {e.printStackTrace();}}@Overridepublic void onServiceDisconnected(ComponentName name) {}}}
混合调用的服务的生命周期:
服务长期后台运行,又想调用服务的方法:
1.start方式开启服务(保证服务长期后台运行)
2.bind方式绑定服务(保证调用服务的方法)
3.unbind解除绑定服务
4.stopService停止服务。
加载大图片到内存
<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" > <Button android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="加载图片到内存" /> <ImageView android:id="@+id/iv" android:layout_width="fill_parent" android:layout_height="fill_parent" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.loadimg" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.READ_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.loadimg.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>
package com.itheima.loadimg;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.BitmapFactory.Options;import android.os.Bundle;import android.view.View;import android.view.WindowManager;import android.widget.ImageView;public class MainActivity extends Activity {private ImageView iv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv = (ImageView) findViewById(R.id.iv);}public void click(View view){//相当消耗内存资源 根据图片的分辨率而定// Bitmap bitmap = BitmapFactory.decodeFile("/mnt/sdcard/photo.jpg");// iv.setImageBitmap(bitmap);//1.得到屏幕的宽高信息WindowManager wm = getWindowManager();int screenWidth = wm.getDefaultDisplay().getWidth();int screenHeight = wm.getDefaultDisplay().getHeight();System.out.println("屏幕宽高:"+screenWidth+"-"+screenHeight);//2.得到图片的宽高。BitmapFactory.Options opts = new Options();//解析位图的附加条件opts.inJustDecodeBounds = true;//不去解析真实的位图,只是获取这个位图的头文件信息Bitmap bitmap = BitmapFactory.decodeFile("/mnt/sdcard/photo.jpg", opts);int bitmapWidth = opts.outWidth;int bitmapHeight = opts.outHeight;System.out.println("图片宽高: "+bitmapWidth+"-"+bitmapHeight);//3.计算缩放比例int dx = bitmapWidth/screenWidth;int dy = bitmapHeight/screenHeight;int scale = 1;if(dx>dy&&dy>1){System.out.println("按照水平方法缩放,缩放比例:"+dx); scale = dx;}if(dy>dx&&dx>1){System.out.println("按照垂直方法缩放,缩放比例:"+dy);scale = dy;}//4.缩放加载图片到内存。opts.inSampleSize = scale;opts.inJustDecodeBounds = false;//真正的去解析这个位图。bitmap = BitmapFactory.decodeFile("/mnt/sdcard/photo.jpg", opts);iv.setImageBitmap(bitmap);}}
创建一个图形的拷贝
<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" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="click" android:text="拷贝一个位图" /> <ImageView android:id="@+id/iv1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv2" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.copybitmap" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.copybitmap.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>
package com.itheima.copybitmap;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;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.view.View;import android.widget.ImageView;public class MainActivity extends Activity {private ImageView iv1,iv2;private Bitmap alterBitmap;private Bitmap srcBmp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv1 = (ImageView) findViewById(R.id.iv1);iv2 = (ImageView) findViewById(R.id.iv2);//给第一个imageview默认设置一个位图srcBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);iv1.setImageBitmap(srcBmp);//创建原图的一个副本。 可修改 创建的是一个空白的图形。alterBitmap = Bitmap.createBitmap(srcBmp.getWidth(), srcBmp.getHeight(),srcBmp.getConfig());}/** * 创建原图 bm的一个拷贝。副本 * @param view */public void click(View view){//1.准备一个画板 在上面放上准备好的 空白的位图Canvas canvas = new Canvas(alterBitmap);//2.准备一个画笔Paint paint = new Paint();paint.setColor(Color.BLACK);//3.画画Matrix m = new Matrix();canvas.drawBitmap(srcBmp, m, paint);iv2.setImageBitmap(alterBitmap);//把原图的副本设置到界面上。}}
图片的放大和缩小
package com.itheima.copybitmap;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;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.view.View;import android.widget.ImageView;public class MainActivity extends Activity {private ImageView iv1,iv2;private Bitmap alterBitmap;private Bitmap srcBmp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv1 = (ImageView) findViewById(R.id.iv1);iv2 = (ImageView) findViewById(R.id.iv2);//给第一个imageview默认设置一个位图srcBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);iv1.setImageBitmap(srcBmp);//创建原图的一个副本。 可修改 创建的是一个空白的图形。alterBitmap = Bitmap.createBitmap(srcBmp.getWidth()*2, srcBmp.getHeight()*2,srcBmp.getConfig());}/** * 创建原图 bm的一个拷贝。副本 * @param view */public void click(View view){//1.准备一个画板 在上面放上准备好的 空白的位图Canvas canvas = new Canvas(alterBitmap);//2.准备一个画笔Paint paint = new Paint();paint.setColor(Color.BLACK);//3.画画Matrix m = new Matrix();m.setScale(2.0f, 2.0f);canvas.drawBitmap(srcBmp, m, paint);iv2.setImageBitmap(alterBitmap);//把原图的副本设置到界面上。}}
图形的旋转和特殊效果
图像的旋转
package com.itheima.copybitmap;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;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.view.View;import android.widget.ImageView;public class MainActivity extends Activity {private ImageView iv1,iv2;private Bitmap alterBitmap;private Bitmap srcBmp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv1 = (ImageView) findViewById(R.id.iv1);iv2 = (ImageView) findViewById(R.id.iv2);//给第一个imageview默认设置一个位图srcBmp = BitmapFactory.decodeResource(getResources(), R.drawable.tomcat);iv1.setImageBitmap(srcBmp);//创建原图的一个副本。 可修改 创建的是一个空白的图形。alterBitmap = Bitmap.createBitmap(srcBmp.getWidth(), srcBmp.getHeight(),srcBmp.getConfig());}/** * 创建原图 bm的一个拷贝。副本 * @param view */public void click(View view){//1.准备一个画板 在上面放上准备好的 空白的位图Canvas canvas = new Canvas(alterBitmap);//2.准备一个画笔Paint paint = new Paint();paint.setColor(Color.BLACK);//3.画画Matrix m = new Matrix();m.setRotate(180, srcBmp.getWidth()/2, srcBmp.getHeight()/2);canvas.drawBitmap(srcBmp, m, paint);iv2.setImageBitmap(alterBitmap);//把原图的副本设置到界面上。}}
图形的镜面效果
package com.itheima.copybitmap;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;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.view.View;import android.widget.ImageView;public class MainActivity extends Activity {private ImageView iv1,iv2;private Bitmap alterBitmap;private Bitmap srcBmp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv1 = (ImageView) findViewById(R.id.iv1);iv2 = (ImageView) findViewById(R.id.iv2);//给第一个imageview默认设置一个位图srcBmp = BitmapFactory.decodeResource(getResources(), R.drawable.tomcat);iv1.setImageBitmap(srcBmp);//创建原图的一个副本。 可修改 创建的是一个空白的图形。alterBitmap = Bitmap.createBitmap(srcBmp.getWidth(), srcBmp.getHeight(),srcBmp.getConfig());}/** * 创建原图 bm的一个拷贝。副本 * @param view */public void click(View view){//1.准备一个画板 在上面放上准备好的 空白的位图Canvas canvas = new Canvas(alterBitmap);//2.准备一个画笔Paint paint = new Paint();paint.setColor(Color.BLACK);//3.画画Matrix m = new Matrix();m.setScale(1.0f, -1.0f);m.postTranslate(0, srcBmp.getHeight());canvas.drawBitmap(srcBmp, m, paint);iv2.setImageBitmap(alterBitmap);//把原图的副本设置到界面上。}}
小游戏-撕衣服
<RelativeLayout 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" tools:context=".MainActivity" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/after" /> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /></RelativeLayout>
package com.itheima.play;import android.media.MediaPlayer;import android.os.Bundle;import android.app.Activity;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.view.Menu;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageView;public class MainActivity extends Activity {private ImageView iv;// 可以修改的位图private Bitmap alertBitmap;private Canvas canvas;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv = (ImageView) findViewById(R.id.iv);Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.pre);// 创建一个空白的原图的拷贝alertBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), bitmap.getConfig());canvas = new Canvas(alertBitmap);Paint paint = new Paint();paint.setColor(Color.BLACK);canvas.drawBitmap(bitmap, new Matrix(), paint);iv.setImageBitmap(alertBitmap);iv.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:// 手指按下屏幕System.out.println("action down");break;case MotionEvent.ACTION_MOVE:// 手指在屏幕上移动int x = (int) event.getX();int y = (int) event.getY();System.out.println("设置("+x+","+y+")透明颜色");for(int i=-4;i<5;i++){for(int j=-4;j<5;j++){try{alertBitmap.setPixel(x+i, y+j, Color.TRANSPARENT);}catch (Exception e) {// TODO: handle exception}}}iv.setImageBitmap(alertBitmap);break;case MotionEvent.ACTION_UP:// 手指离开屏幕MediaPlayer.create(getApplicationContext(), R.raw.higirl).start();break;}return true;//可以重复循环的处理事件}});}}
人脸识别
Google 自带的api, 不好用
国内的网站:
http://www.faceplusplus.com.cn/
- 【黑马Android】(10)绑定的方式调用服务的方法/图片的各种操作/人脸识别
- Android绑定方式开始服务&调用服务当中的方法
- 绑定的方式调用服务的方法
- Android--绑定服务调用服务的方法
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
- 22_绑定的方式调用服务的方法
- android,aidl,绑定远程服务,调用远程服务的方法
- android之绑定服务调用服务的方法
- Android启动服务和绑定服务的方法调用
- android 服务的绑定调用
- 绑定服务&调用服务里面的方法
- 绑定服务:调用服务内部的方法
- andorid,绑定服务,调用服务的方法
- activity以绑定服务的方式开启服务并调用服务里面的方法
- 绑定的方式调用服务的方法和采用接口方式抽取方法
- Android中服务里面方法的调用,开启,绑定
- 绑定服务并调用服务的方法,解除绑定
- 91_采用绑定服务的方式调用服务里面的方法
- Sublime Text 3
- 队列的数组实现
- Astar Round2A 1003 Snacks
- javascript语法之for-in语句
- 【算法】——插入排序
- 【黑马Android】(10)绑定的方式调用服务的方法/图片的各种操作/人脸识别
- 设计模式与代码重构——ios篇
- hdu5701--中位数计数(2016百度之星Round2B/1006)
- Java从控制台中读取数据
- Linux——Redis安装配置
- MongoDB简单入门
- BSG白山极客挑战赛A-数数字
- ORCLE10安装常见配置问题-oui.exe停止工作
- 排序链表