Search in sources :

Example 1 with OnCompositionLoadedListener

use of com.airbnb.lottie.OnCompositionLoadedListener in project lottie-android by airbnb.

the class AnimationFragment method loadUrl.

private void loadUrl(String url) {
    Request request;
    try {
        request = new Request.Builder().url(url).build();
    } catch (IllegalArgumentException e) {
        onLoadError();
        return;
    }
    if (client == null) {
        client = new OkHttpClient();
    }
    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            onLoadError();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (!response.isSuccessful()) {
                onLoadError();
            }
            try {
                JSONObject json = new JSONObject(response.body().string());
                LottieComposition.Factory.fromJson(getResources(), json, new OnCompositionLoadedListener() {

                    @Override
                    public void onCompositionLoaded(LottieComposition composition) {
                        setComposition(composition, "Network Animation");
                    }
                });
            } catch (JSONException e) {
                onLoadError();
            }
        }
    });
}
Also used : Call(okhttp3.Call) OnCompositionLoadedListener(com.airbnb.lottie.OnCompositionLoadedListener) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) JSONException(org.json.JSONException) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) JSONObject(org.json.JSONObject) LottieComposition(com.airbnb.lottie.LottieComposition)

Example 2 with OnCompositionLoadedListener

use of com.airbnb.lottie.OnCompositionLoadedListener in project fluttie by simolus3.

the class FluttiePlugin method loadComposition.

private void loadComposition(final int requestId, String sourceType, String source) throws JSONException {
    final JSONObject object = new JSONObject();
    object.put("request_id", requestId);
    object.put("event_type", "load_composition");
    OnCompositionLoadedListener listener = new OnCompositionLoadedListener() {

        @Override
        public void onCompositionLoaded(@Nullable LottieComposition composition) {
            try {
                if (composition == null) {
                    Log.w("FluttiePlugin", "Could not load composition");
                    object.put("success", false);
                    writeToSink(object);
                    return;
                }
                if (!composition.getWarnings().isEmpty()) {
                    Log.w("FluttiePlugin", "You tried to load a composition that has some warnings, it might not be displayed correctly");
                    for (String warning : composition.getWarnings()) {
                        Log.w("FluttiePlugin", warning);
                    }
                }
                loadedCompositions.append(requestId, composition);
                object.put("success", true);
            } catch (JSONException e) {
                Log.w("FluttiePlugin", "Could not add JSON value to event stream");
            }
            writeToSink(object);
        }
    };
    switch(sourceType) {
        case "inline":
            LottieComposition.Factory.fromJsonString(source, listener);
            break;
        default:
            object.put("success", false);
            writeToSink(object);
    }
}
Also used : OnCompositionLoadedListener(com.airbnb.lottie.OnCompositionLoadedListener) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) Nullable(android.support.annotation.Nullable) LottieComposition(com.airbnb.lottie.LottieComposition)

Example 3 with OnCompositionLoadedListener

use of com.airbnb.lottie.OnCompositionLoadedListener in project lottie-android by airbnb.

the class AnimationFragment method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
        return;
    }
    switch(requestCode) {
        case RC_ASSET:
            final String assetName = data.getStringExtra(EXTRA_ANIMATION_NAME);
            animationView.setImageAssetsFolder(assetFolders.get(assetName));
            LottieComposition.Factory.fromAssetFileName(getContext(), assetName, new OnCompositionLoadedListener() {

                @Override
                public void onCompositionLoaded(LottieComposition composition) {
                    setComposition(composition, assetName);
                }
            });
            break;
        case RC_FILE:
            onFileLoaded(data.getData());
            break;
        case RC_URL:
            break;
    }
}
Also used : OnCompositionLoadedListener(com.airbnb.lottie.OnCompositionLoadedListener) LottieComposition(com.airbnb.lottie.LottieComposition)

Example 4 with OnCompositionLoadedListener

use of com.airbnb.lottie.OnCompositionLoadedListener in project lottie-android by airbnb.

the class AnimationFragment method onFileLoaded.

private void onFileLoaded(final Uri uri) {
    InputStream fis;
    try {
        switch(uri.getScheme()) {
            case "file":
                fis = new FileInputStream(uri.getPath());
                break;
            case "content":
                fis = getContext().getContentResolver().openInputStream(uri);
                break;
            default:
                onLoadError();
                return;
        }
    } catch (FileNotFoundException e) {
        onLoadError();
        return;
    }
    LottieComposition.Factory.fromInputStream(getContext(), fis, new OnCompositionLoadedListener() {

        @Override
        public void onCompositionLoaded(LottieComposition composition) {
            setComposition(composition, uri.getPath());
        }
    });
}
Also used : OnCompositionLoadedListener(com.airbnb.lottie.OnCompositionLoadedListener) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) LottieComposition(com.airbnb.lottie.LottieComposition)

Example 5 with OnCompositionLoadedListener

use of com.airbnb.lottie.OnCompositionLoadedListener in project lottie-android by airbnb.

the class LottieFontViewGroup method init.

private void init() {
    setFocusableInTouchMode(true);
    LottieComposition.Factory.fromAssetFileName(getContext(), "Mobilo/BlinkingCursor.json", new OnCompositionLoadedListener() {

        @Override
        public void onCompositionLoaded(LottieComposition composition) {
            cursorView = new LottieAnimationView(getContext());
            cursorView.setLayoutParams(new LottieFontViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            cursorView.setComposition(composition);
            cursorView.loop(true);
            cursorView.playAnimation();
            addView(cursorView);
        }
    });
}
Also used : OnCompositionLoadedListener(com.airbnb.lottie.OnCompositionLoadedListener) LottieAnimationView(com.airbnb.lottie.LottieAnimationView) LottieComposition(com.airbnb.lottie.LottieComposition)

Aggregations

LottieComposition (com.airbnb.lottie.LottieComposition)8 OnCompositionLoadedListener (com.airbnb.lottie.OnCompositionLoadedListener)8 LottieAnimationView (com.airbnb.lottie.LottieAnimationView)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 ColorDrawable (android.graphics.drawable.ColorDrawable)1 Nullable (android.support.annotation.Nullable)1 View (android.view.View)1 Button (android.widget.Button)1 CompoundButton (android.widget.CompoundButton)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 LottieDrawable (com.airbnb.lottie.LottieDrawable)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Call (okhttp3.Call)1 Callback (okhttp3.Callback)1 OkHttpClient (okhttp3.OkHttpClient)1