【黑马Android】(10)绑定的方式调用服务的方法/图片的各种操作/人脸识别

绑定的方式调用服务的方法

服务的生命周期:

 

一、采用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 {

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	@Override
	public void onCreate() {
		System.out.println("oncreate");
		super.onCreate();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("onstartcommand");
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public 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. 当服务被连接的时候调用 服务别成功 绑定的时候调用
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			System.out.println("在activity里面成功得到了中间人");
			mp = (IMiddlePerson) service;
		}
		//当服务失去连接的时候调用(一般进程挂了,服务被异常杀死)
		@Override
		public 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.实现服务成功绑定的代码 ,返回一个中间人。
	@Override
	public IBinder onBind(Intent arg0) {
		System.out.println("服务被成功绑定了。。。。");
		return new MiddlePerson();
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		System.out.println("onunbind");
		return super.onUnbind(intent);
	}
	
	@Override
	public void onCreate() {
		System.out.println("oncreate");
		super.onCreate();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("onstartcommand");
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public 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 {

	@Override
	protected 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 {

	@Override
	public void onCreate() {
		System.out.println("远程服务被创建了。。。");
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		System.out.println("远程服务被销毁了。");
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return new MiddlePerson();
	}

	private void methodInService(){
		System.out.println("我是远程服务的方法,我被调用了。。。。");
	}
	
	//1.创建一个中间人  远程服务继承的是ipc的一个实现类
	private class MiddlePerson extends IMiddlePerson.Stub{
		@Override
		public 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;

	@Override
	protected 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{
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			iMp = IMiddlePerson.Stub.asInterface(service);
		}
 
		@Override
		public void onServiceDisconnected(ComponentName name) {
			
		}
	}
	
	public void call(View view){
		try {
			iMp.callMethodInService();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	@Override
	protected 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>

远程服务:调用者和服务在不同的工程代码里面。

本地服务:调用者和服务在同一个工程代码里面。

 

每一个应用程序都是运行在自己独立的进程里面的。

进程操作系统分配内存空间的一个单位。进程的数据都是独立的。独立的内存空间。

 

aidlandroid interface definition language  安卓接口定义语言

aidl文件都是公有的,没有访问权限修饰符

IPCinter 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 {

	@Override
	protected 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 {

	@Override
	public 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{
		/**
		 * 调用安全支付的逻辑
		 */
		@Override
		public boolean callPay(long time, String pwd, double money)
				throws RemoteException {
			return pay(time, pwd, money);
		}
		
	}
	
	@Override
	public void onCreate() {
		System.out.println("oncreate支付宝服务被创建,一直在后台运行,检查手机的安全状态");
		super.onCreate();
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("服务onstart");
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		System.out.println("onunbind");
		return super.onUnbind(intent);

	}
	@Override
	public 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;

	@Override
	protected 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{
		@Override
		public 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();
			}
		}

		@Override
		public 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;

	@Override
	protected 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;
	@Override
	protected 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;
	@Override
	protected 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;
	@Override
	protected 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;
	@Override
	protected 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;

	@Override
	protected 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() {
			@Override
			public 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/


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值