Hippy项目源码分析Week13

2021SC@SDUSC

examples/android-demo/java/module

TestModule.java

public class TestModule extends HippyNativeModuleBase {
    final static String CLASSNAME = "TestModule";

    public TestModule(HippyEngineContext context) {
        super(context);
    }

    @HippyMethod(name = "debug")
    public void debug(int instanceid) {
        HippyRootView hippyRootView = mContext.getInstance(instanceid);
        Intent intent = new Intent();
        intent.setClass(hippyRootView.getContext(), BaseActivity.class);
        hippyRootView.getContext().startActivity(intent);
    }


    /***
     * TestModule
     * @param log
     * 自定义了扩展了一个log的接口并且无回调
     */
    @HippyMethod(name = "log")
    public void log(String log) {
        //这里回来的参数可以为java的基础类型,和hippymap与hippyarry,但是前端调用的时候必须对应上
        Log.d("TestModule", log);
    }

    @HippyMethod(name = "helloNative")
    public void helloNative(HippyMap hippyMap) {
        //这里回来的参数可以为java的基础类型,和hippymap与hippyarry,但是前端调用的时候必须对应上
        String hello = hippyMap.getString("hello");
        Log.d("TestModule", hello);
    }

    @HippyMethod(name = "helloNativeWithPromise")
    public void helloNativeWithPromise(HippyMap hippyMap, Promise promise) {
        //这里回来的参数可以为java的基础类型,和hippymap与hippyarry,但是前端调用的时候必须对应上
        String hello = hippyMap.getString("hello");
        Log.d("TestModule", hello);

        if (!TextUtils.isEmpty(hello)) {
            //TODO: 如果模块这里处理成功回调resolve
            HippyMap hippyMap1 = new HippyMap();
            hippyMap1.pushInt("code", 1);
            hippyMap1.pushString("result", "hello i am from native");
            promise.resolve(hippyMap1);
        } else {
            //失败就回调reject
            HippyMap hippyMap1 = new HippyMap();
            hippyMap1.pushInt("code", -1);
            promise.reject(hippyMap1);
        }

    }
}

examples/android-demo/java/adapter

MyImageLoader.java

public class MyImageLoader extends HippyImageLoader
{
	private Timer mTimer = new Timer("MyImageLoader", true);
	private Handler mHandler = new Handler(Looper.getMainLooper());
	private Context myContext;

	public MyImageLoader(Context context) {
		myContext = context;
	}

	@Override
	public void destroyIfNeed() {
		mHandler = null;
		mTimer = null;
		myContext = null;
	}

	@SuppressWarnings({"UnusedAssignment", "rawtypes"})
	private void runFetchImageOnMianThread(final String url, final Callback requestCallback, final Object paramsObj) {
		Object propsObj;
		if (paramsObj instanceof Map) {
			//noinspection rawtypes
			propsObj = ((Map)paramsObj).get(HippyImageView.IMAGE_PROPS);
		} else {
			propsObj = paramsObj;
		}

		HippyMap props = (propsObj instanceof HippyMap) ? (HippyMap)propsObj : new HippyMap();

		int width = 0;
		int height = 0;
		int repeatCount;
		boolean isGif;
		String resizeMode = "";
		String imageType = "";

		if (props.containsKey(NodeProps.STYLE)) {
			HippyMap styles = props.getMap(NodeProps.STYLE);
			if (styles != null) {
				width = Math.round(PixelUtil.dp2px(styles.getDouble(NodeProps.WIDTH)));
				height = Math.round(PixelUtil.dp2px(styles.getDouble(NodeProps.HEIGHT)));
				resizeMode = styles.getString(NodeProps.RESIZE_MODE);
			}
		}

		imageType = props.getString(NodeProps.CUSTOM_PROP_IMAGE_TYPE);
		repeatCount = props.getInt(NodeProps.REPEAT_COUNT);
		isGif = props.getBoolean(NodeProps.CUSTOM_PROP_ISGIF);

		//noinspection unchecked
		Glide.with(myContext).load(url).into(new SimpleTarget() {
			@Override
			public void onResourceReady(final Object object, GlideAnimation glideAnimation) {
				final HippyDrawable hippyTarget = new HippyDrawable();
				if (object instanceof GifDrawable)
				{
					mTimer.schedule(new TimerTask()
					{
						@Override
						public void run() {
							// 这里setData会解码,耗时,所以在子线程做
							hippyTarget.setData(((GifDrawable) object).getData());
							mHandler.post(new Runnable() {
								@Override
								public void run() {
									requestCallback.onRequestSuccess(hippyTarget);
								}
							});
						}
					}, 0);
				}
				else if (object instanceof GlideBitmapDrawable)
				{
					mTimer.schedule(new TimerTask()
					{
						@Override
						public void run() {
							// 这里setData会解码,耗时,所以在子线程做
							hippyTarget.setData(((GlideBitmapDrawable) object).getBitmap());
							mHandler.post(new Runnable() {
								@Override
								public void run() {
									requestCallback.onRequestSuccess(hippyTarget);
								}
							});
						}
					}, 0);
				}
			}

			@Override
			public void onLoadFailed(Exception e, Drawable errorDrawable) {
				requestCallback.onRequestFail(e, null);
			}
		});
	}

	// 网络图片加载,异步加载
	@Override
	public void fetchImage(final String url, final Callback requestCallback, final Object paramsObj) {
		Looper looper = Looper.myLooper();
		if (looper == Looper.getMainLooper()) {
			runFetchImageOnMianThread(url, requestCallback, paramsObj);
		} else {
			Handler mainHandler = new Handler(Looper.getMainLooper());
			Runnable task = new Runnable() {
				@Override
				public void run() {
					runFetchImageOnMianThread(url, requestCallback, paramsObj);
				}
			};
			mainHandler.post(task);
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值