androidx.core.graphics.drawable.RoundedBitmapDrawable Java Examples

The following examples show how to use androidx.core.graphics.drawable.RoundedBitmapDrawable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TextCardView.java    From tv-samples with Apache License 2.0 6 votes vote down vote up
public void updateUi(Card card) {
    TextView extraText = findViewById(R.id.extra_text);
    TextView primaryText = findViewById(R.id.primary_text);
    final ImageView imageView = findViewById(R.id.main_image);

    extraText.setText(card.getExtraText());
    primaryText.setText(card.getTitle());

    // Create a rounded drawable.
    int resourceId = card.getLocalImageResourceId(getContext());
    Bitmap bitmap = BitmapFactory
            .decodeResource(getContext().getResources(), resourceId);
    RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getContext().getResources(), bitmap);
    drawable.setAntiAlias(true);
    drawable.setCornerRadius(
            Math.max(bitmap.getWidth(), bitmap.getHeight()) / 2.0f);
    imageView.setImageDrawable(drawable);
}
 
Example #2
Source File: BindingAdapters.java    From Jockey with Apache License 2.0 6 votes vote down vote up
@BindingAdapter(value = { "bitmap", "cornerRadius" })
public static void bindRoundedBitmap(ImageView imageView, Bitmap bitmap, float cornerRadius) {
    ViewUtils.whenLaidOut(imageView, () -> {
        if (imageView.getWidth() == 0 || imageView.getHeight() == 0) {
            Timber.e("ImageView has a dimension of 0", new RuntimeException());
            imageView.setImageBitmap(bitmap);
        } else {
            RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory
                    .create(imageView.getResources(), bitmap);

            float scaleX = bitmap.getWidth() / imageView.getWidth();
            float scaleY = bitmap.getHeight() / imageView.getHeight();
            drawable.setCornerRadius(Math.min(scaleX, scaleY) * cornerRadius);

            imageView.setImageDrawable(drawable);
        }
    });
}
 
Example #3
Source File: ImageUtils.java    From SendBird-Android with MIT License 6 votes vote down vote up
/**
 * Crops image into a circle that fits within the ImageView.
 */
public static void displayRoundImageFromUrl(final Context context, final String url, final ImageView imageView) {
    RequestOptions myOptions = new RequestOptions()
            .centerCrop()
            .dontAnimate();

    Glide.with(context)
            .asBitmap()
            .apply(myOptions)
            .load(url)
            .into(new BitmapImageViewTarget(imageView) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    imageView.setImageDrawable(circularBitmapDrawable);
                }
            });
}
 
Example #4
Source File: ImageUtils.java    From SendBird-Android with MIT License 6 votes vote down vote up
/**
 * Crops image into a circle that fits within the ImageView.
 */
public static void displayRoundImageFromUrl(final Context context, final String url, final ImageView imageView) {
    RequestOptions myOptions = new RequestOptions()
            .centerCrop()
            .dontAnimate();

    Glide.with(context)
            .asBitmap()
            .apply(myOptions)
            .load(url)
            .into(new BitmapImageViewTarget(imageView) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    imageView.setImageDrawable(circularBitmapDrawable);
                }
            });
}
 
Example #5
Source File: GlideEngine.java    From PictureSelector with Apache License 2.0 6 votes vote down vote up
/**
 * 加载相册目录
 *
 * @param context   上下文
 * @param url       图片路径
 * @param imageView 承载图片ImageView
 */
@Override
public void loadFolderImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView) {
    Glide.with(context)
            .asBitmap()
            .load(url)
            .override(180, 180)
            .centerCrop()
            .sizeMultiplier(0.5f)
            .apply(new RequestOptions().placeholder(R.drawable.picture_image_placeholder))
            .into(new BitmapImageViewTarget(imageView) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.
                                    create(context.getResources(), resource);
                    circularBitmapDrawable.setCornerRadius(8);
                    imageView.setImageDrawable(circularBitmapDrawable);
                }
            });
}
 
Example #6
Source File: GlideEngine.java    From react-native-syan-image-picker with MIT License 6 votes vote down vote up
/**
 * 加载相册目录
 *
 * @param context   上下文
 * @param url       图片路径
 * @param imageView 承载图片ImageView
 */
@Override
public void loadFolderImage(@NonNull Context context, @NonNull String url, @NonNull ImageView imageView) {
    Glide.with(context)
            .asBitmap()
            .load(url)
            .override(180, 180)
            .centerCrop()
            .sizeMultiplier(0.5f)
            .apply(new RequestOptions().placeholder(R.drawable.picture_image_placeholder))
            .into(new BitmapImageViewTarget(imageView) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.
                                    create(context.getResources(), resource);
                    circularBitmapDrawable.setCornerRadius(8);
                    imageView.setImageDrawable(circularBitmapDrawable);
                }
            });
}
 
Example #7
Source File: UserIcon.java    From FCM-for-Mojo with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 把 Bitmap 变圆。
 *
 * @param context Context
 * @param bitmap  要处理的 Bitmap
 * @return 圆形的 Bitmap
 */
public static Bitmap clipToRound(Context context, Bitmap bitmap) {
    final RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
    drawable.setAntiAlias(true);
    drawable.setCircular(true);
    drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());

    bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.draw(canvas);

    return bitmap;
}
 
Example #8
Source File: MyAdapter.java    From PixImagePicker with Apache License 2.0 5 votes vote down vote up
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    //Uri imageUri = Uri.fromFile(new File(list.get(position)));// For files on device
    File f = new File(list.get(position));
    Bitmap bitmap;
    if (f.getAbsolutePath().endsWith("mp4")) {
        ((Holder) holder).play.setVisibility(View.VISIBLE);
        bitmap = ThumbnailUtils.createVideoThumbnail(f.getAbsolutePath(), MediaStore.Video.Thumbnails.MINI_KIND);
    } else {
        ((Holder) holder).play.setVisibility(View.GONE);
        bitmap = new BitmapDrawable(context.getResources(), f.getAbsolutePath()).getBitmap();
    }
    RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
    final float roundPx = (float) bitmap.getWidth() * 0.06f;
    roundedBitmapDrawable.setCornerRadius(roundPx);
    ((Holder) holder).iv.setImageDrawable(roundedBitmapDrawable);
    ((Holder) holder).iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(f.getAbsolutePath()));
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    intent.setDataAndType(Uri.parse(f.getAbsolutePath()), Files.probeContentType(f.toPath()));
                } else {
                    intent.setData(Uri.parse(f.getAbsolutePath()));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            context.startActivity(intent);
        }
    });
    /*Bitmap scaled = com.fxn.utility.Utility.getScaledBitmap(
        500f, com.fxn.utility.Utility.rotate(d,list.get(position).getOrientation()));*/

}
 
Example #9
Source File: CastAdapter.java    From android-popular-movies-app with Apache License 2.0 5 votes vote down vote up
/**
 * This method will take a Cast object as input and use that cast to display the appropriate
 * text and an image within a list item.
 *
 * @param cast The cast object
 */
 void bind(Cast cast) {
    // The complete profile image url
    String profile = IMAGE_BASE_URL + IMAGE_FILE_SIZE + cast.getProfilePath();
    // Load image with Picasso library
    Picasso.with(itemView.getContext())
            .load(profile)
            // Create circular avatars
            // Reference: @see "https://stackoverflow.com/questions/26112150/android-create
            // -circular-image-with-picasso"
            .into(mCastItemBinding.ivCast, new Callback() {
                @Override
                public void onSuccess() {
                    Bitmap imageBitmap = ((BitmapDrawable) mCastItemBinding.ivCast.getDrawable())
                            .getBitmap();
                    RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(
                            itemView.getContext().getResources(), // to determine density
                            imageBitmap); // image to round
                    drawable.setCircular(true);
                    mCastItemBinding.ivCast.setImageDrawable(drawable);
                }

                @Override
                public void onError() {
                    mCastItemBinding.ivCast.setImageResource(R.drawable.account_circle);
                }
            });

    // Set the cast name and character name to the TextViews
    mCastItemBinding.setCast(cast);
}
 
Example #10
Source File: CircleImageView.java    From ColorPickerDialog with Apache License 2.0 5 votes vote down vote up
@Override
public void onDraw(Canvas canvas) {
    Bitmap image = ImageUtils.drawableToBitmap(getDrawable());
    if (image != null) {
        int size = Math.min(getWidth(), getHeight());
        image = ThumbnailUtils.extractThumbnail(image, size, size);

        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), image);

        roundedBitmapDrawable.setCornerRadius(size / 2);
        roundedBitmapDrawable.setAntiAlias(true);

        canvas.drawBitmap(ImageUtils.drawableToBitmap(roundedBitmapDrawable), 0, 0, paint);
    }
}
 
Example #11
Source File: CircleImageView.java    From line-sdk-android with Apache License 2.0 5 votes vote down vote up
@Override
public void setImageDrawable(@Nullable Drawable drawable) {
    if (drawable == null) {
        super.setImageDrawable(null);
        return;
    }

    Bitmap imageBitmap = ((BitmapDrawable) drawable).getBitmap();
    RoundedBitmapDrawable imageDrawable = RoundedBitmapDrawableFactory.create(getContext().getResources(), imageBitmap);
    imageDrawable.setCircular(true);
    imageDrawable.setCornerRadius(Math.max(imageBitmap.getWidth(), imageBitmap.getHeight()) / 2.0f);
    super.setImageDrawable(imageDrawable);
}
 
Example #12
Source File: AppInstalledPresenter.java    From LeanbackTvSample with MIT License 5 votes vote down vote up
@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
    if (item instanceof AppInfo) {
        ViewHolder vh = (ViewHolder) viewHolder;
        if (((AppInfo) item).icon != null) {
            Bitmap bitmap = getBitmapFromDrawable(((AppInfo) item).icon);//适配Android 8.0
            RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), bitmap);
            drawable.setCornerRadius(FontDisplayUtil.dip2px(mContext, 10));
            vh.mIvAppIcon.setImageDrawable(drawable);
        }
        if (!TextUtils.isEmpty(((AppInfo) item).name)) {
            vh.mTvAppName.setText(((AppInfo) item).name);
        }
    }
}
 
Example #13
Source File: CharacterCardView.java    From tv-samples with Apache License 2.0 5 votes vote down vote up
public void updateUi(Card card) {
    TextView primaryText = findViewById(R.id.primary_text);
    final ImageView imageView = findViewById(R.id.main_image);

    primaryText.setText(card.getTitle());
    if (card.getLocalImageResourceName() != null) {
        int resourceId = card.getLocalImageResourceId(getContext());
        Bitmap bitmap = BitmapFactory
                .decodeResource(getContext().getResources(), resourceId);
        RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getContext().getResources(), bitmap);
        drawable.setAntiAlias(true);
        drawable.setCornerRadius(Math.max(bitmap.getWidth(), bitmap.getHeight()) / 2.0f);
        imageView.setImageDrawable(drawable);
    }
}
 
Example #14
Source File: GroupConversation.java    From NaviBee with GNU General Public License v3.0 4 votes vote down vote up
public Drawable getRoundIconDrawable(Resources r) {
    RoundedBitmapDrawable d = RoundedBitmapDrawableFactory.create(r, iconBitmap);
    d.setAntiAlias(true);
    d.setCircular(true);
    return d;
}
 
Example #15
Source File: ViewUtil.java    From BaseProject with Apache License 2.0 4 votes vote down vote up
public static void imageViewRounded(ImageView iv, int imageResId, int roundedRadius) {
    Resources res = iv.getResources();
    RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(res, BitmapFactory.decodeResource(res, imageResId));
    roundedBitmapDrawable.setCornerRadius(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, roundedRadius, res.getDisplayMetrics()));
    iv.setImageDrawable(roundedBitmapDrawable);
}
 
Example #16
Source File: PeopleRecyclerAdapter.java    From mage-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onBindViewHolder(final PersonViewHolder viewHolder, int i) {
    final User user = people.get(i);

    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (personClickListener != null) {
                personClickListener.onPersonClick(user);
            }
        }
    });

    UserLocal userLocal = user.getUserLocal();
    GlideApp.with(context)
            .asBitmap()
            .load(Avatar.Companion.forUser(user))
            .fallback(R.drawable.ic_person_gray_24dp)
            .centerCrop()
            .into(new BitmapImageViewTarget(viewHolder.avatar) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    viewHolder.avatar.setImageDrawable(circularBitmapDrawable);
                }
            });

    GlideApp.with(context)
            .load(userLocal.getLocalIconPath())
            .centerCrop()
            .into(viewHolder.icon);

    viewHolder.name.setText(user.getDisplayName());

    Collection<Team> userTeams = teamHelper.getTeamsByUser(user);
    userTeams.retainAll(eventTeams);
    Collection<String> teamNames = Collections2.transform(userTeams, new Function<Team, String>() {
        @Override
        public String apply(Team team) {
            return team.getName();
        }
    });

    viewHolder.teams.setText(StringUtils.join(teamNames, ", "));

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (personClickListener != null) {
                PersonViewHolder holder = (PersonViewHolder) view.getTag();
                int position = holder.getLayoutPosition();
                personClickListener.onPersonClick(people.get(position));
            }
        }
    };
}
 
Example #17
Source File: FileViewModel.java    From Jockey with Apache License 2.0 4 votes vote down vote up
private Drawable makeCircular(Bitmap image) {
    RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(), image);
    drawable.setCircular(true);
    return drawable;
}