Search in sources :

Example 1 with AnimatedFileDrawable

use of org.telegram.ui.Components.AnimatedFileDrawable in project Telegram-FOSS by Telegram-FOSS-Team.

the class PhotoViewer method switchToPip.

private void switchToPip(boolean fromGesture) {
    if (videoPlayer == null || !textureUploaded || !checkInlinePermissions() || changingTextureView || switchingInlineMode || isInline) {
        return;
    }
    if (PipInstance != null) {
        PipInstance.destroyPhotoViewer();
    }
    openedFullScreenVideo = false;
    PipInstance = Instance;
    Instance = null;
    switchingInlineMode = true;
    isVisible = false;
    AndroidUtilities.cancelRunOnUIThread(hideActionBarRunnable);
    if (currentPlaceObject != null && !currentPlaceObject.imageReceiver.getVisible()) {
        currentPlaceObject.imageReceiver.setVisible(true, true);
        AnimatedFileDrawable animation = currentPlaceObject.imageReceiver.getAnimation();
        if (animation != null) {
            Bitmap bitmap = animation.getAnimatedBitmap();
            if (bitmap != null) {
                try {
                    Bitmap src = videoTextureView.getBitmap(bitmap.getWidth(), bitmap.getHeight());
                    Canvas canvas = new Canvas(bitmap);
                    canvas.drawBitmap(src, 0, 0, null);
                    src.recycle();
                } catch (Throwable e) {
                    FileLog.e(e);
                }
            }
            animation.seekTo(videoPlayer.getCurrentPosition(), true);
            if (fromGesture) {
                currentPlaceObject.imageReceiver.setAlpha(0);
                ImageReceiver imageReceiver = currentPlaceObject.imageReceiver;
                ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1f);
                valueAnimator.addUpdateListener((a) -> imageReceiver.setAlpha((Float) a.getAnimatedValue()));
                valueAnimator.addListener(new AnimatorListenerAdapter() {

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        imageReceiver.setAlpha(1f);
                    }
                });
                valueAnimator.setDuration(250);
                valueAnimator.start();
            }
            currentPlaceObject.imageReceiver.setAllowStartAnimation(true);
            currentPlaceObject.imageReceiver.startAnimation();
        }
    }
    if (Build.VERSION.SDK_INT >= 21) {
        pipAnimationInProgress = true;
        org.telegram.ui.Components.Rect rect = PipVideoView.getPipRect(aspectRatioFrameLayout.getAspectRatio());
        float scale = rect.width / videoTextureView.getWidth();
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1f);
        float fromX = videoTextureView.getTranslationX();
        float fromY = videoTextureView.getTranslationY() + translationY;
        float fromY2 = textureImageView.getTranslationY() + translationY;
        float toX = rect.x;
        float toX2 = rect.x - aspectRatioFrameLayout.getX() + getLeftInset();
        float toY = rect.y;
        float toY2 = rect.y - aspectRatioFrameLayout.getY();
        textureImageView.setTranslationY(fromY2);
        videoTextureView.setTranslationY(fromY);
        if (firstFrameView != null) {
            firstFrameView.setTranslationY(fromY);
        }
        translationY = 0;
        containerView.invalidate();
        CubicBezierInterpolator interpolator;
        if (fromGesture) {
            if (fromY < toY2) {
                interpolator = new CubicBezierInterpolator(0.5, 0, 0.9, 0.9);
            } else {
                interpolator = new CubicBezierInterpolator(0, 0.5, 0.9, 0.9);
            }
        } else {
            interpolator = null;
        }
        valueAnimator.addUpdateListener(animation -> {
            float xValue = (float) animation.getAnimatedValue();
            float yValue = interpolator == null ? xValue : interpolator.getInterpolation(xValue);
            textureImageView.setTranslationX(fromX * (1f - xValue) + toX * xValue);
            textureImageView.setTranslationY(fromY2 * (1f - yValue) + toY * yValue);
            videoTextureView.setTranslationX(fromX * (1f - xValue) + (toX2) * xValue);
            videoTextureView.setTranslationY(fromY * (1f - yValue) + (toY2) * yValue);
            if (firstFrameView != null) {
                firstFrameView.setTranslationX(videoTextureView.getTranslationX());
                firstFrameView.setTranslationY(videoTextureView.getTranslationY());
                firstFrameView.setScaleX(videoTextureView.getScaleX());
                firstFrameView.setScaleY(videoTextureView.getScaleY());
            }
        });
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(ObjectAnimator.ofFloat(textureImageView, View.SCALE_X, scale), ObjectAnimator.ofFloat(textureImageView, View.SCALE_Y, scale), ObjectAnimator.ofFloat(videoTextureView, View.SCALE_X, scale), ObjectAnimator.ofFloat(videoTextureView, View.SCALE_Y, scale), ObjectAnimator.ofInt(backgroundDrawable, AnimationProperties.COLOR_DRAWABLE_ALPHA, 0), valueAnimator);
        if (fromGesture) {
            animatorSet.setInterpolator(CubicBezierInterpolator.EASE_OUT);
            animatorSet.setDuration(300);
        } else {
            animatorSet.setInterpolator(new DecelerateInterpolator());
            animatorSet.setDuration(250);
        }
        animatorSet.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                pipAnimationInProgress = false;
                switchToInlineRunnable.run();
            }
        });
        animatorSet.start();
        if (!fromGesture) {
            toggleActionBar(false, true, new ActionBarToggleParams().enableStatusBarAnimation(false).enableTranslationAnimation(false).animationDuration(250).animationInterpolator(new DecelerateInterpolator()));
        }
    } else {
        switchToInlineRunnable.run();
        dismissInternal();
    }
    if (parentChatActivity != null) {
        parentChatActivity.getFragmentView().invalidate();
    }
}
Also used : DecelerateInterpolator(android.view.animation.DecelerateInterpolator) Canvas(android.graphics.Canvas) AnimatorSet(android.animation.AnimatorSet) ValueAnimator(android.animation.ValueAnimator) ImageReceiver(org.telegram.messenger.ImageReceiver) Bitmap(android.graphics.Bitmap) Animator(android.animation.Animator) DefaultItemAnimator(androidx.recyclerview.widget.DefaultItemAnimator) ValueAnimator(android.animation.ValueAnimator) ObjectAnimator(android.animation.ObjectAnimator) ViewPropertyAnimator(android.view.ViewPropertyAnimator) AnimatedFileDrawable(org.telegram.ui.Components.AnimatedFileDrawable) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) CubicBezierInterpolator(org.telegram.ui.Components.CubicBezierInterpolator)

Example 2 with AnimatedFileDrawable

use of org.telegram.ui.Components.AnimatedFileDrawable in project Telegram-FOSS by Telegram-FOSS-Team.

the class PinchToZoomHelper method startZoom.

public void startZoom(View child, ImageReceiver image, MessageObject messageObject) {
    this.child = child;
    this.messageObject = messageObject;
    if (overlayView == null) {
        overlayView = new ZoomOverlayView(parentView.getContext());
        overlayView.setFocusable(false);
        overlayView.setFocusableInTouchMode(false);
        overlayView.setEnabled(false);
    }
    if (fullImage == null) {
        fullImage = new ImageReceiver();
        fullImage.setCrossfadeAlpha((byte) 2);
        fullImage.setCrossfadeWithOldImage(false);
        fullImage.onAttachedToWindow();
    }
    inOverlayMode = true;
    parentView.addView(overlayView);
    finishProgress = 1f;
    progressToFullView = 0f;
    setFullImage(messageObject);
    imageX = image.getImageX();
    imageY = image.getImageY();
    imageHeight = image.getImageHeight();
    imageWidth = image.getImageWidth();
    fullImageHeight = image.getBitmapHeight();
    fullImageWidth = image.getBitmapWidth();
    if (fullImageHeight / fullImageWidth != imageHeight / imageWidth) {
        if (fullImageHeight / fullImageWidth < imageHeight / imageWidth) {
            fullImageWidth = fullImageWidth / fullImageHeight * imageHeight;
            fullImageHeight = imageHeight;
        } else {
            fullImageHeight = fullImageHeight / fullImageWidth * imageWidth;
            fullImageWidth = imageWidth;
        }
    } else {
        fullImageHeight = imageHeight;
        fullImageWidth = imageWidth;
    }
    if (messageObject != null && messageObject.isVideo() && MediaController.getInstance().isPlayingMessage(messageObject)) {
        isHardwareVideo = true;
        MediaController.getInstance().setTextureView(overlayView.videoTextureView, overlayView.aspectRatioFrameLayout, overlayView.videoPlayerContainer, true);
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) overlayView.videoPlayerContainer.getLayoutParams();
        overlayView.videoPlayerContainer.setTag(R.id.parent_tag, image);
        if (layoutParams.width != image.getImageWidth() || layoutParams.height != image.getImageHeight()) {
            overlayView.aspectRatioFrameLayout.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
            layoutParams.width = (int) image.getImageWidth();
            layoutParams.height = (int) image.getImageHeight();
            overlayView.videoPlayerContainer.setLayoutParams(layoutParams);
        }
        overlayView.videoTextureView.setScaleX(1f);
        overlayView.videoTextureView.setScaleY(1f);
        if (callback != null) {
            overlayView.backupImageView.setImageBitmap(callback.getCurrentTextureView().getBitmap((int) fullImageWidth, (int) fullImageHeight));
            overlayView.backupImageView.setSize((int) fullImageWidth, (int) fullImageHeight);
            overlayView.backupImageView.getImageReceiver().setRoundRadius(image.getRoundRadius());
        }
        overlayView.videoPlayerContainer.setVisibility(View.VISIBLE);
    } else {
        isHardwareVideo = false;
        this.childImage = new ImageReceiver();
        this.childImage.onAttachedToWindow();
        Drawable drawable = image.getDrawable();
        this.childImage.setImageBitmap(drawable);
        if (drawable instanceof AnimatedFileDrawable) {
            ((AnimatedFileDrawable) drawable).addSecondParentView(overlayView);
            ((AnimatedFileDrawable) drawable).setInvalidateParentViewWithSecond(true);
        }
        this.childImage.setImageCoords(imageX, imageY, imageWidth, imageHeight);
        this.childImage.setRoundRadius(image.getRoundRadius());
        this.fullImage.setRoundRadius(image.getRoundRadius());
        overlayView.videoPlayerContainer.setVisibility(View.GONE);
    }
    if (callback != null) {
        callback.onZoomStarted(messageObject);
    }
    enterProgress = 0f;
}
Also used : ImageReceiver(org.telegram.messenger.ImageReceiver) AnimatedFileDrawable(org.telegram.ui.Components.AnimatedFileDrawable) FrameLayout(android.widget.FrameLayout) AspectRatioFrameLayout(com.google.android.exoplayer2.ui.AspectRatioFrameLayout) AnimatedFileDrawable(org.telegram.ui.Components.AnimatedFileDrawable) Drawable(android.graphics.drawable.Drawable)

Example 3 with AnimatedFileDrawable

use of org.telegram.ui.Components.AnimatedFileDrawable in project Telegram-FOSS by Telegram-FOSS-Team.

the class ImageReceiver method drawDrawable.

private void drawDrawable(Canvas canvas, Drawable drawable, int alpha, BitmapShader shader, int orientation, int isPressed) {
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        Paint paint;
        if (shader != null) {
            paint = roundPaint;
        } else {
            paint = bitmapDrawable.getPaint();
        }
        if (Build.VERSION.SDK_INT >= 29) {
            if (blendMode != null && gradientShader == null) {
                paint.setBlendMode((BlendMode) blendMode);
            } else {
                paint.setBlendMode(null);
            }
        }
        boolean hasFilter = paint != null && paint.getColorFilter() != null;
        if (hasFilter && isPressed == 0) {
            if (shader != null) {
                roundPaint.setColorFilter(null);
            } else if (staticThumbDrawable != drawable) {
                bitmapDrawable.setColorFilter(null);
            }
        } else if (!hasFilter && isPressed != 0) {
            if (isPressed == 1) {
                if (shader != null) {
                    roundPaint.setColorFilter(selectedColorFilter);
                } else {
                    bitmapDrawable.setColorFilter(selectedColorFilter);
                }
            } else {
                if (shader != null) {
                    roundPaint.setColorFilter(selectedGroupColorFilter);
                } else {
                    bitmapDrawable.setColorFilter(selectedGroupColorFilter);
                }
            }
        }
        if (colorFilter != null && gradientShader == null) {
            if (shader != null) {
                roundPaint.setColorFilter(colorFilter);
            } else {
                bitmapDrawable.setColorFilter(colorFilter);
            }
        }
        int bitmapW;
        int bitmapH;
        if (bitmapDrawable instanceof AnimatedFileDrawable || bitmapDrawable instanceof RLottieDrawable) {
            if (orientation % 360 == 90 || orientation % 360 == 270) {
                bitmapW = bitmapDrawable.getIntrinsicHeight();
                bitmapH = bitmapDrawable.getIntrinsicWidth();
            } else {
                bitmapW = bitmapDrawable.getIntrinsicWidth();
                bitmapH = bitmapDrawable.getIntrinsicHeight();
            }
        } else {
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap != null && bitmap.isRecycled()) {
                return;
            }
            if (orientation % 360 == 90 || orientation % 360 == 270) {
                bitmapW = bitmap.getHeight();
                bitmapH = bitmap.getWidth();
            } else {
                bitmapW = bitmap.getWidth();
                bitmapH = bitmap.getHeight();
            }
        }
        float realImageW = imageW - sideClip * 2;
        float realImageH = imageH - sideClip * 2;
        float scaleW = imageW == 0 ? 1.0f : (bitmapW / realImageW);
        float scaleH = imageH == 0 ? 1.0f : (bitmapH / realImageH);
        if (shader != null) {
            if (isAspectFit) {
                float scale = Math.max(scaleW, scaleH);
                bitmapW /= scale;
                bitmapH /= scale;
                drawRegion.set(imageX + (imageW - bitmapW) / 2, imageY + (imageH - bitmapH) / 2, imageX + (imageW + bitmapW) / 2, imageY + (imageH + bitmapH) / 2);
                if (isVisible) {
                    roundPaint.setShader(shader);
                    shaderMatrix.reset();
                    shaderMatrix.setTranslate(drawRegion.left, drawRegion.top);
                    shaderMatrix.preScale(1.0f / scale, 1.0f / scale);
                    shader.setLocalMatrix(shaderMatrix);
                    roundPaint.setAlpha(alpha);
                    roundRect.set(drawRegion);
                    if (isRoundRect) {
                        try {
                            if (roundRadius[0] == 0) {
                                canvas.drawRect(roundRect, roundPaint);
                            } else {
                                canvas.drawRoundRect(roundRect, roundRadius[0], roundRadius[0], roundPaint);
                            }
                        } catch (Exception e) {
                            onBitmapException(bitmapDrawable);
                            FileLog.e(e);
                        }
                    } else {
                        for (int a = 0; a < roundRadius.length; a++) {
                            radii[a * 2] = roundRadius[a];
                            radii[a * 2 + 1] = roundRadius[a];
                        }
                        roundPath.reset();
                        roundPath.addRoundRect(roundRect, radii, Path.Direction.CW);
                        roundPath.close();
                        canvas.drawPath(roundPath, roundPaint);
                    }
                }
            } else {
                if (legacyCanvas != null) {
                    roundRect.set(0, 0, legacyBitmap.getWidth(), legacyBitmap.getHeight());
                    legacyCanvas.drawBitmap(gradientBitmap, null, roundRect, null);
                    legacyCanvas.drawBitmap(bitmapDrawable.getBitmap(), null, roundRect, legacyPaint);
                }
                if (shader == imageShader && gradientShader != null) {
                    if (composeShader != null) {
                        roundPaint.setShader(composeShader);
                    } else {
                        roundPaint.setShader(legacyShader);
                    }
                } else {
                    roundPaint.setShader(shader);
                }
                float scale = 1.0f / Math.min(scaleW, scaleH);
                roundRect.set(imageX + sideClip, imageY + sideClip, imageX + imageW - sideClip, imageY + imageH - sideClip);
                if (Math.abs(scaleW - scaleH) > 0.0005f) {
                    if (bitmapW / scaleH > realImageW) {
                        bitmapW /= scaleH;
                        drawRegion.set(imageX - (bitmapW - realImageW) / 2, imageY, imageX + (bitmapW + realImageW) / 2, imageY + realImageH);
                    } else {
                        bitmapH /= scaleW;
                        drawRegion.set(imageX, imageY - (bitmapH - realImageH) / 2, imageX + realImageW, imageY + (bitmapH + realImageH) / 2);
                    }
                } else {
                    drawRegion.set(imageX, imageY, imageX + realImageW, imageY + realImageH);
                }
                if (isVisible) {
                    shaderMatrix.reset();
                    shaderMatrix.setTranslate(drawRegion.left + sideClip, drawRegion.top + sideClip);
                    if (orientation == 90) {
                        shaderMatrix.preRotate(90);
                        shaderMatrix.preTranslate(0, -drawRegion.width());
                    } else if (orientation == 180) {
                        shaderMatrix.preRotate(180);
                        shaderMatrix.preTranslate(-drawRegion.width(), -drawRegion.height());
                    } else if (orientation == 270) {
                        shaderMatrix.preRotate(270);
                        shaderMatrix.preTranslate(-drawRegion.height(), 0);
                    }
                    shaderMatrix.preScale(scale, scale);
                    if (isRoundVideo) {
                        float postScale = (realImageW + AndroidUtilities.roundMessageInset * 2) / realImageW;
                        shaderMatrix.postScale(postScale, postScale, drawRegion.centerX(), drawRegion.centerY());
                    }
                    if (legacyShader != null) {
                        legacyShader.setLocalMatrix(shaderMatrix);
                    }
                    shader.setLocalMatrix(shaderMatrix);
                    if (composeShader != null) {
                        int bitmapW2 = gradientBitmap.getWidth();
                        int bitmapH2 = gradientBitmap.getHeight();
                        float scaleW2 = imageW == 0 ? 1.0f : (bitmapW2 / realImageW);
                        float scaleH2 = imageH == 0 ? 1.0f : (bitmapH2 / realImageH);
                        if (Math.abs(scaleW2 - scaleH2) > 0.0005f) {
                            if (bitmapW2 / scaleH2 > realImageW) {
                                bitmapW2 /= scaleH2;
                                drawRegion.set(imageX - (bitmapW2 - realImageW) / 2, imageY, imageX + (bitmapW2 + realImageW) / 2, imageY + realImageH);
                            } else {
                                bitmapH2 /= scaleW2;
                                drawRegion.set(imageX, imageY - (bitmapH2 - realImageH) / 2, imageX + realImageW, imageY + (bitmapH2 + realImageH) / 2);
                            }
                        } else {
                            drawRegion.set(imageX, imageY, imageX + realImageW, imageY + realImageH);
                        }
                        scale = 1.0f / Math.min(imageW == 0 ? 1.0f : (bitmapW2 / realImageW), imageH == 0 ? 1.0f : (bitmapH2 / realImageH));
                        shaderMatrix.reset();
                        shaderMatrix.setTranslate(drawRegion.left + sideClip, drawRegion.top + sideClip);
                        shaderMatrix.preScale(scale, scale);
                        gradientShader.setLocalMatrix(shaderMatrix);
                    }
                    roundPaint.setAlpha(alpha);
                    if (isRoundRect) {
                        try {
                            if (roundRadius[0] == 0) {
                                canvas.drawRect(roundRect, roundPaint);
                            } else {
                                canvas.drawRoundRect(roundRect, roundRadius[0], roundRadius[0], roundPaint);
                            }
                        } catch (Exception e) {
                            onBitmapException(bitmapDrawable);
                            FileLog.e(e);
                        }
                    } else {
                        for (int a = 0; a < roundRadius.length; a++) {
                            radii[a * 2] = roundRadius[a];
                            radii[a * 2 + 1] = roundRadius[a];
                        }
                        roundPath.reset();
                        roundPath.addRoundRect(roundRect, radii, Path.Direction.CW);
                        roundPath.close();
                        canvas.drawPath(roundPath, roundPaint);
                    }
                }
            }
        } else {
            if (isAspectFit) {
                float scale = Math.max(scaleW, scaleH);
                canvas.save();
                bitmapW /= scale;
                bitmapH /= scale;
                drawRegion.set(imageX + (imageW - bitmapW) / 2.0f, imageY + (imageH - bitmapH) / 2.0f, imageX + (imageW + bitmapW) / 2.0f, imageY + (imageH + bitmapH) / 2.0f);
                bitmapDrawable.setBounds((int) drawRegion.left, (int) drawRegion.top, (int) drawRegion.right, (int) drawRegion.bottom);
                if (bitmapDrawable instanceof AnimatedFileDrawable) {
                    ((AnimatedFileDrawable) bitmapDrawable).setActualDrawRect(drawRegion.left, drawRegion.top, drawRegion.width(), drawRegion.height());
                }
                if (isVisible) {
                    try {
                        bitmapDrawable.setAlpha(alpha);
                        bitmapDrawable.draw(canvas);
                    } catch (Exception e) {
                        onBitmapException(bitmapDrawable);
                        FileLog.e(e);
                    }
                }
                canvas.restore();
            } else {
                if (Math.abs(scaleW - scaleH) > 0.00001f) {
                    canvas.save();
                    canvas.clipRect(imageX, imageY, imageX + imageW, imageY + imageH);
                    if (orientation % 360 != 0) {
                        if (centerRotation) {
                            canvas.rotate(orientation, imageW / 2, imageH / 2);
                        } else {
                            canvas.rotate(orientation, 0, 0);
                        }
                    }
                    if (bitmapW / scaleH > imageW) {
                        bitmapW /= scaleH;
                        drawRegion.set(imageX - (bitmapW - imageW) / 2.0f, imageY, imageX + (bitmapW + imageW) / 2.0f, imageY + imageH);
                    } else {
                        bitmapH /= scaleW;
                        drawRegion.set(imageX, imageY - (bitmapH - imageH) / 2.0f, imageX + imageW, imageY + (bitmapH + imageH) / 2.0f);
                    }
                    if (bitmapDrawable instanceof AnimatedFileDrawable) {
                        ((AnimatedFileDrawable) bitmapDrawable).setActualDrawRect(imageX, imageY, imageW, imageH);
                    }
                    if (orientation % 360 == 90 || orientation % 360 == 270) {
                        float width = drawRegion.width() / 2;
                        float height = drawRegion.height() / 2;
                        float centerX = drawRegion.centerX();
                        float centerY = drawRegion.centerY();
                        bitmapDrawable.setBounds((int) (centerX - height), (int) (centerY - width), (int) (centerX + height), (int) (centerY + width));
                    } else {
                        bitmapDrawable.setBounds((int) drawRegion.left, (int) drawRegion.top, (int) drawRegion.right, (int) drawRegion.bottom);
                    }
                    if (isVisible) {
                        try {
                            if (Build.VERSION.SDK_INT >= 29) {
                                if (blendMode != null) {
                                    bitmapDrawable.getPaint().setBlendMode((BlendMode) blendMode);
                                } else {
                                    bitmapDrawable.getPaint().setBlendMode(null);
                                }
                            }
                            bitmapDrawable.setAlpha(alpha);
                            bitmapDrawable.draw(canvas);
                        } catch (Exception e) {
                            onBitmapException(bitmapDrawable);
                            FileLog.e(e);
                        }
                    }
                    canvas.restore();
                } else {
                    canvas.save();
                    if (orientation % 360 != 0) {
                        if (centerRotation) {
                            canvas.rotate(orientation, imageW / 2, imageH / 2);
                        } else {
                            canvas.rotate(orientation, 0, 0);
                        }
                    }
                    drawRegion.set(imageX, imageY, imageX + imageW, imageY + imageH);
                    if (isRoundVideo) {
                        drawRegion.inset(-AndroidUtilities.roundMessageInset, -AndroidUtilities.roundMessageInset);
                    }
                    if (bitmapDrawable instanceof AnimatedFileDrawable) {
                        ((AnimatedFileDrawable) bitmapDrawable).setActualDrawRect(imageX, imageY, imageW, imageH);
                    }
                    if (orientation % 360 == 90 || orientation % 360 == 270) {
                        float width = drawRegion.width() / 2;
                        float height = drawRegion.height() / 2;
                        float centerX = drawRegion.centerX();
                        float centerY = drawRegion.centerY();
                        bitmapDrawable.setBounds((int) (centerX - height), (int) (centerY - width), (int) (centerX + height), (int) (centerY + width));
                    } else {
                        bitmapDrawable.setBounds((int) drawRegion.left, (int) drawRegion.top, (int) drawRegion.right, (int) drawRegion.bottom);
                    }
                    if (isVisible) {
                        try {
                            if (Build.VERSION.SDK_INT >= 29) {
                                if (blendMode != null) {
                                    bitmapDrawable.getPaint().setBlendMode((BlendMode) blendMode);
                                } else {
                                    bitmapDrawable.getPaint().setBlendMode(null);
                                }
                            }
                            bitmapDrawable.setAlpha(alpha);
                            bitmapDrawable.draw(canvas);
                        } catch (Exception e) {
                            onBitmapException(bitmapDrawable);
                            FileLog.e(e);
                        }
                    }
                    canvas.restore();
                }
            }
        }
    } else {
        if (isAspectFit) {
            int bitmapW = drawable.getIntrinsicWidth();
            int bitmapH = drawable.getIntrinsicHeight();
            float realImageW = imageW - sideClip * 2;
            float realImageH = imageH - sideClip * 2;
            float scaleW = imageW == 0 ? 1.0f : (bitmapW / realImageW);
            float scaleH = imageH == 0 ? 1.0f : (bitmapH / realImageH);
            float scale = Math.max(scaleW, scaleH);
            bitmapW /= scale;
            bitmapH /= scale;
            drawRegion.set(imageX + (imageW - bitmapW) / 2.0f, imageY + (imageH - bitmapH) / 2.0f, imageX + (imageW + bitmapW) / 2.0f, imageY + (imageH + bitmapH) / 2.0f);
        } else {
            drawRegion.set(imageX, imageY, imageX + imageW, imageY + imageH);
        }
        drawable.setBounds((int) drawRegion.left, (int) drawRegion.top, (int) drawRegion.right, (int) drawRegion.bottom);
        if (isVisible) {
            try {
                drawable.setAlpha(alpha);
                drawable.draw(canvas);
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) AnimatedFileDrawable(org.telegram.ui.Components.AnimatedFileDrawable) RLottieDrawable(org.telegram.ui.Components.RLottieDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 4 with AnimatedFileDrawable

use of org.telegram.ui.Components.AnimatedFileDrawable in project Telegram-FOSS by Telegram-FOSS-Team.

the class ImageReceiver method draw.

public boolean draw(Canvas canvas) {
    boolean result = false;
    if (gradientBitmap != null && currentImageKey != null) {
        canvas.save();
        canvas.clipRect(imageX, imageY, imageX + imageW, imageY + imageH);
        canvas.drawColor(0xff000000);
    }
    try {
        Drawable drawable = null;
        AnimatedFileDrawable animation = getAnimation();
        RLottieDrawable lottieDrawable = getLottieAnimation();
        boolean animationNotReady = animation != null && !animation.hasBitmap() || lottieDrawable != null && !lottieDrawable.hasBitmap();
        if (animation != null) {
            animation.setRoundRadius(roundRadius);
        }
        if (lottieDrawable != null) {
            lottieDrawable.setCurrentParentView(parentView);
        }
        if ((animation != null || lottieDrawable != null) && !animationNotReady && !animationReadySent) {
            animationReadySent = true;
            if (delegate != null) {
                delegate.onAnimationReady(this);
            }
        }
        int orientation = 0;
        BitmapShader shaderToUse = null;
        if (!forcePreview && currentMediaDrawable != null && !animationNotReady) {
            drawable = currentMediaDrawable;
            shaderToUse = mediaShader;
            orientation = imageOrientation;
        } else if (!forcePreview && currentImageDrawable != null && (!animationNotReady || currentMediaDrawable != null)) {
            drawable = currentImageDrawable;
            shaderToUse = imageShader;
            orientation = imageOrientation;
            animationNotReady = false;
        } else if (crossfadeImage != null && !crossfadingWithThumb) {
            drawable = crossfadeImage;
            shaderToUse = crossfadeShader;
            orientation = imageOrientation;
        } else if (staticThumbDrawable instanceof BitmapDrawable) {
            drawable = staticThumbDrawable;
            if (useRoundForThumb && thumbShader == null) {
                updateDrawableRadius(staticThumbDrawable);
            }
            shaderToUse = thumbShader;
            orientation = thumbOrientation;
        } else if (currentThumbDrawable != null) {
            drawable = currentThumbDrawable;
            shaderToUse = thumbShader;
            orientation = thumbOrientation;
        }
        if (drawable != null) {
            if (crossfadeAlpha != 0) {
                if (previousAlpha != 1f && (drawable == currentImageDrawable || drawable == currentMediaDrawable) && staticThumbDrawable != null) {
                    if (useRoundForThumb && thumbShader == null) {
                        updateDrawableRadius(staticThumbDrawable);
                    }
                    drawDrawable(canvas, staticThumbDrawable, (int) (overrideAlpha * 255), thumbShader, orientation);
                }
                if (crossfadeWithThumb && animationNotReady) {
                    drawDrawable(canvas, drawable, (int) (overrideAlpha * 255), shaderToUse, orientation);
                } else {
                    if (crossfadeWithThumb && currentAlpha != 1.0f) {
                        Drawable thumbDrawable = null;
                        BitmapShader thumbShaderToUse = null;
                        if (drawable == currentImageDrawable || drawable == currentMediaDrawable) {
                            if (crossfadeImage != null) {
                                thumbDrawable = crossfadeImage;
                                thumbShaderToUse = crossfadeShader;
                            } else if (currentThumbDrawable != null) {
                                thumbDrawable = currentThumbDrawable;
                                thumbShaderToUse = thumbShader;
                            } else if (staticThumbDrawable != null) {
                                thumbDrawable = staticThumbDrawable;
                                if (useRoundForThumb && thumbShader == null) {
                                    updateDrawableRadius(staticThumbDrawable);
                                }
                                thumbShaderToUse = thumbShader;
                            }
                        } else if (drawable == currentThumbDrawable || drawable == crossfadeImage) {
                            if (staticThumbDrawable != null) {
                                thumbDrawable = staticThumbDrawable;
                                if (useRoundForThumb && thumbShader == null) {
                                    updateDrawableRadius(staticThumbDrawable);
                                }
                                thumbShaderToUse = thumbShader;
                            }
                        } else if (drawable == staticThumbDrawable) {
                            if (crossfadeImage != null) {
                                thumbDrawable = crossfadeImage;
                                thumbShaderToUse = crossfadeShader;
                            }
                        }
                        if (thumbDrawable != null) {
                            int alpha;
                            if (thumbDrawable instanceof SvgHelper.SvgDrawable || thumbDrawable instanceof Emoji.EmojiDrawable) {
                                alpha = (int) (overrideAlpha * 255 * (1.0f - currentAlpha));
                            } else {
                                alpha = (int) (overrideAlpha * previousAlpha * 255);
                            }
                            drawDrawable(canvas, thumbDrawable, alpha, thumbShaderToUse, thumbOrientation);
                            if (alpha != 255 && thumbDrawable instanceof Emoji.EmojiDrawable) {
                                thumbDrawable.setAlpha(255);
                            }
                        }
                    }
                    drawDrawable(canvas, drawable, (int) (overrideAlpha * currentAlpha * 255), shaderToUse, orientation);
                }
            } else {
                drawDrawable(canvas, drawable, (int) (overrideAlpha * 255), shaderToUse, orientation);
            }
            checkAlphaAnimation(animationNotReady && crossfadeWithThumb);
            result = true;
        } else if (staticThumbDrawable != null) {
            drawDrawable(canvas, staticThumbDrawable, (int) (overrideAlpha * 255), null, thumbOrientation);
            checkAlphaAnimation(animationNotReady);
            result = true;
        } else {
            checkAlphaAnimation(animationNotReady);
        }
        if (drawable == null && animationNotReady && parentView != null) {
            parentView.invalidate();
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
    if (gradientBitmap != null && currentImageKey != null) {
        canvas.restore();
    }
    return result;
}
Also used : AnimatedFileDrawable(org.telegram.ui.Components.AnimatedFileDrawable) LoadingStickerDrawable(org.telegram.ui.Components.LoadingStickerDrawable) AnimatedFileDrawable(org.telegram.ui.Components.AnimatedFileDrawable) Drawable(android.graphics.drawable.Drawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) RecyclableDrawable(org.telegram.ui.Components.RecyclableDrawable) RLottieDrawable(org.telegram.ui.Components.RLottieDrawable) RLottieDrawable(org.telegram.ui.Components.RLottieDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) BitmapShader(android.graphics.BitmapShader) Paint(android.graphics.Paint)

Example 5 with AnimatedFileDrawable

use of org.telegram.ui.Components.AnimatedFileDrawable in project Telegram-FOSS by Telegram-FOSS-Team.

the class ImageReceiver method setImageBitmapByKey.

protected boolean setImageBitmapByKey(Drawable drawable, String key, int type, boolean memCache, int guid) {
    if (drawable == null || key == null || currentGuid != guid) {
        return false;
    }
    if (type == TYPE_IMAGE) {
        if (!key.equals(currentImageKey)) {
            return false;
        }
        if (!(drawable instanceof AnimatedFileDrawable)) {
            ImageLoader.getInstance().incrementUseCount(currentImageKey);
        } else {
            AnimatedFileDrawable animatedFileDrawable = (AnimatedFileDrawable) drawable;
            animatedFileDrawable.setStartEndTime(startTime, endTime);
            if (animatedFileDrawable.isWebmSticker) {
                ImageLoader.getInstance().incrementUseCount(currentImageKey);
            }
        }
        currentImageDrawable = drawable;
        if (drawable instanceof ExtendedBitmapDrawable) {
            imageOrientation = ((ExtendedBitmapDrawable) drawable).getOrientation();
        }
        updateDrawableRadius(drawable);
        if (isVisible && (!memCache && !forcePreview || forceCrossfade) && crossfadeDuration != 0) {
            boolean allowCorssfade = true;
            if (currentMediaDrawable instanceof AnimatedFileDrawable && ((AnimatedFileDrawable) currentMediaDrawable).hasBitmap()) {
                allowCorssfade = false;
            } else if (currentImageDrawable instanceof RLottieDrawable) {
                allowCorssfade = staticThumbDrawable instanceof LoadingStickerDrawable || staticThumbDrawable instanceof SvgHelper.SvgDrawable || staticThumbDrawable instanceof Emoji.EmojiDrawable;
            }
            if (allowCorssfade && (currentThumbDrawable != null || staticThumbDrawable != null || forceCrossfade)) {
                if (currentThumbDrawable != null && staticThumbDrawable != null) {
                    previousAlpha = currentAlpha;
                } else {
                    previousAlpha = 1f;
                }
                currentAlpha = 0.0f;
                lastUpdateAlphaTime = System.currentTimeMillis();
                crossfadeWithThumb = crossfadeImage != null || currentThumbDrawable != null || staticThumbDrawable != null;
            }
        } else {
            currentAlpha = 1.0f;
            previousAlpha = 1f;
        }
    } else if (type == TYPE_MEDIA) {
        if (!key.equals(currentMediaKey)) {
            return false;
        }
        if (!(drawable instanceof AnimatedFileDrawable)) {
            ImageLoader.getInstance().incrementUseCount(currentMediaKey);
        } else {
            AnimatedFileDrawable animatedFileDrawable = (AnimatedFileDrawable) drawable;
            animatedFileDrawable.setStartEndTime(startTime, endTime);
            if (animatedFileDrawable.isWebmSticker) {
                ImageLoader.getInstance().incrementUseCount(currentImageKey);
            }
        }
        currentMediaDrawable = drawable;
        updateDrawableRadius(drawable);
        if (currentImageDrawable == null) {
            boolean allowCorssfade = true;
            if (!memCache && !forcePreview || forceCrossfade) {
                if (currentThumbDrawable == null && staticThumbDrawable == null || currentAlpha == 1.0f || forceCrossfade) {
                    if (currentThumbDrawable != null && staticThumbDrawable != null) {
                        previousAlpha = currentAlpha;
                    } else {
                        previousAlpha = 1f;
                    }
                    currentAlpha = 0.0f;
                    lastUpdateAlphaTime = System.currentTimeMillis();
                    crossfadeWithThumb = crossfadeImage != null || currentThumbDrawable != null || staticThumbDrawable != null;
                }
            } else {
                currentAlpha = 1.0f;
                previousAlpha = 1f;
            }
        }
    } else if (type == TYPE_THUMB) {
        if (currentThumbDrawable != null) {
            return false;
        }
        if (!forcePreview) {
            AnimatedFileDrawable animation = getAnimation();
            if (animation != null && animation.hasBitmap()) {
                return false;
            }
            if (currentImageDrawable != null && !(currentImageDrawable instanceof AnimatedFileDrawable) || currentMediaDrawable != null && !(currentMediaDrawable instanceof AnimatedFileDrawable)) {
                return false;
            }
        }
        if (!key.equals(currentThumbKey)) {
            return false;
        }
        ImageLoader.getInstance().incrementUseCount(currentThumbKey);
        currentThumbDrawable = drawable;
        if (drawable instanceof ExtendedBitmapDrawable) {
            thumbOrientation = ((ExtendedBitmapDrawable) drawable).getOrientation();
        }
        updateDrawableRadius(drawable);
        if (!memCache && crossfadeAlpha != 2) {
            if (currentParentObject instanceof MessageObject && ((MessageObject) currentParentObject).isRoundVideo() && ((MessageObject) currentParentObject).isSending()) {
                currentAlpha = 1.0f;
                previousAlpha = 1f;
            } else {
                currentAlpha = 0.0f;
                previousAlpha = 1f;
                lastUpdateAlphaTime = System.currentTimeMillis();
                crossfadeWithThumb = staticThumbDrawable != null;
            }
        } else {
            currentAlpha = 1.0f;
            previousAlpha = 1f;
        }
    }
    if (delegate != null) {
        delegate.didSetImage(this, currentImageDrawable != null || currentThumbDrawable != null || staticThumbDrawable != null || currentMediaDrawable != null, currentImageDrawable == null && currentMediaDrawable == null, memCache);
    }
    if (drawable instanceof AnimatedFileDrawable) {
        AnimatedFileDrawable fileDrawable = (AnimatedFileDrawable) drawable;
        fileDrawable.setUseSharedQueue(useSharedAnimationQueue);
        if (attachedToWindow) {
            fileDrawable.addParent(parentView);
        }
        if (allowStartAnimation && currentOpenedLayerFlags == 0) {
            fileDrawable.start();
        }
        fileDrawable.setAllowDecodeSingleFrame(allowDecodeSingleFrame);
        animationReadySent = false;
        if (parentView != null) {
            parentView.invalidate();
        }
    } else if (drawable instanceof RLottieDrawable) {
        RLottieDrawable fileDrawable = (RLottieDrawable) drawable;
        fileDrawable.addParentView(parentView);
        if (allowStartLottieAnimation && (!fileDrawable.isHeavyDrawable() || currentOpenedLayerFlags == 0)) {
            fileDrawable.start();
        }
        fileDrawable.setAllowDecodeSingleFrame(true);
        fileDrawable.setAutoRepeat(autoRepeat);
        animationReadySent = false;
    }
    if (parentView != null) {
        if (invalidateAll) {
            parentView.invalidate();
        } else {
            parentView.invalidate((int) imageX, (int) imageY, (int) (imageX + imageW), (int) (imageY + imageH));
        }
    }
    return true;
}
Also used : LoadingStickerDrawable(org.telegram.ui.Components.LoadingStickerDrawable) AnimatedFileDrawable(org.telegram.ui.Components.AnimatedFileDrawable) RLottieDrawable(org.telegram.ui.Components.RLottieDrawable)

Aggregations

AnimatedFileDrawable (org.telegram.ui.Components.AnimatedFileDrawable)31 RLottieDrawable (org.telegram.ui.Components.RLottieDrawable)17 Paint (android.graphics.Paint)14 Bitmap (android.graphics.Bitmap)11 Drawable (android.graphics.drawable.Drawable)11 BitmapDrawable (android.graphics.drawable.BitmapDrawable)10 SuppressLint (android.annotation.SuppressLint)9 TextPaint (android.text.TextPaint)9 MessageObject (org.telegram.messenger.MessageObject)6 ClippingImageView (org.telegram.ui.Components.ClippingImageView)6 LoadingStickerDrawable (org.telegram.ui.Components.LoadingStickerDrawable)6 TextureView (android.view.TextureView)5 View (android.view.View)5 HorizontalScrollView (android.widget.HorizontalScrollView)5 ImageView (android.widget.ImageView)5 TextView (android.widget.TextView)5 RecyclerView (androidx.recyclerview.widget.RecyclerView)5 ImageReceiver (org.telegram.messenger.ImageReceiver)5 Canvas (android.graphics.Canvas)3 StaticLayout (android.text.StaticLayout)3