您的位置:首页 > 移动开发 > Android开发

热更新.9图下发_编译使用篇(android原生与脚本语言混合式开发)

2016-08-05 11:46 656 查看
热更新.9图下发_编译使用篇(android原生与脚本语言混合式开发)

在实际开发中,混合式开发是现在比较普遍也是很实用的一种开发方式,android原生与脚本语言开发是比较流行的一种方式,在混合开发中,我们很多时候都需要去下发图片,点九图下发读取不出来就变成了,比较普遍的问题。那么如何解决这个问题呢?

这个时候你需要去编译这个点九图,如何编译这个点九图呢?

1.简单粗暴方法

这种方法需要依赖开发工具,现在的AS和ES开发工具都支持该方法

1)创建一个android项目(android.jar支持环境)

2)把图片放入res资源目录中

3)然后使用这张图片,比如在xml中写入

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.keduchi.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/abc_ab_bottom_solid_dark_holo"
android:text="@string/hello_world" />

</RelativeLayout>


在这里我们使用了这张点九图,您可以选择运行或者只是选择运行却不真正的运行成功。

我们就会看到,如图



bin目录下的res中就会有我们的点九图,这样编译后的点九图和之前没有编译的点九图有什么区别呢?





上图就是编译后和编译之前的图。

2.类似于方法1

1)将你的点9图片放到项目的res/drawable-**目录下;

2)得到加好图片之后的apk(eclipse右击运行,或者eclipse右击->Android Tools->Export …或者使用独 有的编译环境编译,反正得到apk即可)

3)用解压软件解压apk文件,解压之后即可找到对应的res/drawable/你所需要的点9图片

此时可以发现我原本3*3的图片变成了1*1的图片了,而且size也变小了很多。

通过该方法获取的编译后的点九图和方法一获取到的点九图是一样的

3.aapt.exe编译

通过aapt.exe工具编译点九图

1)首先找到aapt.exe的目录,aapt.exe在我们的android SDK中

sdk目录下的build-tools下的android-4.4w目录下部分在

sdk目录下的platform-tools目录下



2)放入图片

编译.9.png: 调用系统的aapt.ext工具对包含png的程序进行处理,zip包,解压zip得到的.9.png即编译过的.9.png

建一个java工程并将android.jar和AndroidManifest.xml放到工程目录下,建一个图片目录res/drawable

3)执行命令

aapt p -M E:\workspace\CompileNinePatch\AndroidManifest.xml -S E:\workspace\CompileNinePatch\res -I E:\workspace\CompileNinePatch\android.jar -F C:\Users\liuyt\Desktop\temp.zip

4)像上面一样操作,图片就会通过zip的形式保存在桌面上,通过如上就可以获取到编译后的点九图。

是不是比方法1和方法2都负责呢?本人比较喜欢简单粗暴的方法,故推荐1

编译好了点九图下面就需要使用点九图,通过热更新下发的图片要如何被脚本语言使用呢?

下面小编提供一个图片处理类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.NinePatch;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.NinePatchDrawable;
/**图片处理类**/
public class ImageUtil {

/**
* 防止decode大图时抛出outofmemery
*
* @param uri
* @param width
* @param height
* @return
* @throws IOException
*/
public static Object safeDecodeStream(String path, int width, int height) {
Bitmap bitmap = null;
String str = null;
try {
File file = new File(path);

int scale = 1;
BitmapFactory.Options options = null;
InputStream in = null;
if (width > 0 && height > 0) {
options = new BitmapFactory.Options();
in =
4000
new FileInputStream(file);
if (width > 0 || height > 0) {
// Decode image size without loading all data into memory
options.inJustDecodeBounds = true;

try {
BitmapFactory.decodeStream(in, null, options);
} catch (OutOfMemoryError e) {

}
in.close();
int w = options.outWidth;
int h = options.outHeight;
if (w > width && h > height) {
float orignialScale = w / (float) h;
float specifiedScale = width / (float) height;

if (orignialScale > specifiedScale) { // 原始宽高比大于指定宽高比,按高度比例取
scale = h / height;
} else if (orignialScale < specifiedScale) {
scale = w / width;
} else {
scale = 1;
}
}
}

// Decode with inSampleSize option
options.inJustDecodeBounds = false;
options.inSampleSize = scale;
}
in = new FileInputStream(file);
bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(in, null, options);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
in.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
} catch (Exception ex) {

} catch (OutOfMemoryError e) {
// TODO: handle exception
}
if (bitmap == null)
return str;
else
return bitmap;
}

public static Bitmap bm;

/**
* 防止decode大图时抛出outofmemery
*
* @param uri
* @param width
* @param height
* @return
* @throws IOException
*/
@SuppressWarnings("deprecation")
public static Object safeDecodeNinePatchStream(String path, int width, int height) {
Bitmap bitmap = null;
//        Log.e("ImageUtil:safeDecodeNinePatchStream", "path is "+path);
try {
File file = new File(path);           //获取path路径下面的图片
InputStream in = null;
in = new FileInputStream(file);
bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(in);
byte[] chunks = bitmap.getNinePatchChunk();
boolean result = NinePatch.isNinePatchChunk(chunks);
if(result){
Rect rect = new Rect(0, 0, width, height);
NinePatchDrawable patchy = new NinePatchDrawable(bitmap, bitmap.getNinePatchChunk(), rect, null);
return patchy;
}else{
BitmapDrawable bd = new BitmapDrawable(bitmap);
return bd;
}

} catch (OutOfMemoryError e) {
e.printStackTrace();
}
in.close();
} catch (FileNotFoundException e) {
Logger.e("tag", e);
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception ex) {

} catch (OutOfMemoryError e) {
// TODO: handle exception
}
return null;
}
}


在脚本语言中如何调用这个方法呢?

function setImageResource(view,path,id,width,height)
local ImageLoader = luajava.bindClass("com.tencent.pandora.tool.ImageUtil");
local bitmap = ImageLoader:safeDecodeStream(path,width, height);
if bitmap~= nil then
view:setImageBitmap(bitmap)
else
view:setImageResource(id)
end
end

--点九图设置方法
--@param view imageview控件
--@param path 热更新图片目录
--@param width 控件宽度
--@param height 控件高度
function setNinePatchDrawable(view,path,id,width,height)
-- body
local drawable = ImageLoader:safeDecodeNinePatchStream(path, width, height)
if view ~= nil then
view:setBackgroundDrawable(drawable)
end
end
function setBackgroundResource(view,path,id,width,height)
local ImageLoader = luajava.bindClass("com.tencent.pandora.tool.ImageUtil");
local bitmap = ImageLoader:safeDecodeStream(path,width, height);
local drawable = nil;
if bitmap~= nil then
drawable = luajava.newInstance("android.graphics.drawable.BitmapDrawable", bitmap);
end
if drawable~= nil then
view:setBackground(drawable)
else
view:setBackgroundResource(id)
end
end


后记:

该文章为小遍原创,转载请写明出处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐