Java Code Examples for android.graphics.Bitmap#sameAs()

The following examples show how to use android.graphics.Bitmap#sameAs() . 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: Utils.java    From ProgressButton with Apache License 2.0 6 votes vote down vote up
public static boolean sameBitmap(Context context, Drawable drawable, int resourceId) {
    Drawable otherDrawable = ContextCompat.getDrawable(context, resourceId);
    if (drawable == null || otherDrawable == null) {
        return false;
    }
    if (drawable instanceof StateListDrawable && otherDrawable instanceof StateListDrawable) {
        drawable = drawable.getCurrent();
        otherDrawable = otherDrawable.getCurrent();
    }
    if (drawable instanceof BitmapDrawable) {
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        Bitmap otherBitmap = ((BitmapDrawable) otherDrawable).getBitmap();
        return bitmap.sameAs(otherBitmap);
    }
    return false;
}
 
Example 2
Source File: LauncherModel.java    From TurboLauncher with Apache License 2.0 6 votes vote down vote up
void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
	boolean needSave = false;
	try {
		if (data != null) {
			Bitmap saved = BitmapFactory.decodeByteArray(data, 0,
					data.length);
			Bitmap loaded = info.getIcon(mIconCache);
			needSave = !saved.sameAs(loaded);
		} else {
			needSave = true;
		}
	} catch (Exception e) {
		needSave = true;
	}
	if (needSave) {
		updateItemInDatabase(context, info);
	}
}
 
Example 3
Source File: CustomMatchers.java    From friendspell with Apache License 2.0 6 votes vote down vote up
private static boolean sameBitmap(Context context, Drawable drawable, int resourceId) {
  if (resourceId == 0) {
    return (drawable == null);
  }
  Drawable otherDrawable = ContextCompat.getDrawable(context, resourceId);
  if (drawable == null || otherDrawable == null) {
    return false;
  }
  if (drawable instanceof StateListDrawable) {
    drawable = drawable.getCurrent();
  }
  if (otherDrawable instanceof StateListDrawable) {
    otherDrawable = otherDrawable.getCurrent();
  }
  if (drawable instanceof BitmapDrawable) {
    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    Bitmap otherBitmap = ((BitmapDrawable) otherDrawable).getBitmap();
    return bitmap.sameAs(otherBitmap);
  }
  return false;
}
 
Example 4
Source File: LauncherModel.java    From LB-Launcher with Apache License 2.0 6 votes vote down vote up
void updateSavedIcon(Context context, ShortcutInfo info, byte[] data) {
    boolean needSave = false;
    try {
        if (data != null) {
            Bitmap saved = BitmapFactory.decodeByteArray(data, 0, data.length);
            Bitmap loaded = info.getIcon(mIconCache);
            needSave = !saved.sameAs(loaded);
        } else {
            needSave = true;
        }
    } catch (Exception e) {
        needSave = true;
    }
    if (needSave) {
        Log.d(TAG, "going to save icon bitmap for info=" + info);
        // This is slower than is ideal, but this only happens once
        // or when the app is updated with a new icon.
        updateItemInDatabase(context, info);
    }
}
 
Example 5
Source File: BitmapLoadUtils.java    From EasyPhotos with Apache License 2.0 5 votes vote down vote up
public static Bitmap transformBitmap(@NonNull Bitmap bitmap, @NonNull Matrix transformMatrix) {
    try {
        Bitmap converted = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), transformMatrix, true);
        if (!bitmap.sameAs(converted)) {
            bitmap = converted;
        }
    } catch (OutOfMemoryError error) {
        Log.e(TAG, "transformBitmap: ", error);
    }
    return bitmap;
}
 
Example 6
Source File: BitmapLoadUtils.java    From Matisse-Kotlin with Apache License 2.0 5 votes vote down vote up
public static Bitmap transformBitmap(@NonNull Bitmap bitmap, @NonNull Matrix transformMatrix) {
    try {
        Bitmap converted = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), transformMatrix, true);
        if (!bitmap.sameAs(converted)) {
            bitmap = converted;
        }
    } catch (OutOfMemoryError error) {
        Log.e(TAG, "transformBitmap: ", error);
    }
    return bitmap;
}
 
Example 7
Source File: BitmapLoadUtils.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
public static Bitmap transformBitmap(@NonNull Bitmap bitmap, @NonNull Matrix transformMatrix) {
    try {
        Bitmap converted = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), transformMatrix, true);
        if (!bitmap.sameAs(converted)) {
            bitmap = converted;
        }
    } catch (OutOfMemoryError error) {
        Log.e(TAG, "transformBitmap: ", error);
    }
    return bitmap;
}
 
Example 8
Source File: Util.java    From Android-Custom-Keyboard with Apache License 2.0 5 votes vote down vote up
public static boolean isLangSupported(Context context, String text) {
    int sdk = android.os.Build.VERSION.SDK_INT;
    int w = 200, h = 80;
    Resources resources = context.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
    Bitmap orig = bitmap.copy(conf, false);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.rgb(0, 0, 0));
    paint.setTextSize((int) (14 * scale));

    // draw text to the Canvas center
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 2;
    int y = (bitmap.getHeight() + bounds.height()) / 2;

    canvas.drawText(text, x, y, paint);
    boolean res = false;
    if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
        res = !(orig == bitmap);
    } else {
        res = !orig.sameAs(bitmap);
    }
    orig.recycle();
    bitmap.recycle();
    return res;
}
 
Example 9
Source File: EmojiDrawable.java    From aurora-imui with MIT License 5 votes vote down vote up
public void setBitmap(Bitmap bitmap) {
    assertMainThread();
    if(bitmap == null) {
        return;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1 && bitmap.sameAs(bmp)){
        return;
    }
    bmp = bitmap;
    invalidateSelf();
}
 
Example 10
Source File: TypefaceUtils.java    From AppCompat-Extension-Library with Apache License 2.0 5 votes vote down vote up
/**
 * Compares two typefaces and returns true if all letters of the English alphabet of the typefaces are the same.
 * If the method returns false it is likely that the typefaces are not the same, yet not certain.
 * <p>
 * NOTE: This only works starting with API level 14 and comes with a small performance penalty.
 * Only use if you have to and cache the result if you need it later.
 *
 * @param typeface1 The first typeface to be compared.
 * @param typeface2 The second typeface to be compared.
 * @return True if the typefaces are the same. False otherwise.
 * @since 0.1.1
 * @deprecated
 */
@Deprecated
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public static boolean sameAs(Typeface typeface1, Typeface typeface2) {
    // Handle null as param.
    if (typeface1 == null) {
        return typeface2 == null;
    } else if (typeface2 == null) {
        return false; //result of typeface1 == null
    }

    // Check if the letters of the English alphabet of the typefaces are the same.
    Paint paint = new Paint();
    paint.setTypeface(typeface1);
    Rect bounds = new Rect();
    paint.getTextBounds(TEXT, 0, TEXT.length(), bounds);
    Bitmap bitmap1 = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ALPHA_8);
    Canvas canvas = new Canvas(bitmap1);
    canvas.drawText(TEXT, 0, 0, paint);

    paint.setTypeface(typeface2);
    paint.getTextBounds(TEXT, 0, TEXT.length(), bounds);
    Bitmap bitmap2 = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ALPHA_8);
    canvas.setBitmap(bitmap2);
    canvas.drawText(TEXT, 0, 0, paint);

    return bitmap1.sameAs(bitmap2);
}
 
Example 11
Source File: UrlStreamOpenerTests.java    From pixate-freestyle-android with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private boolean checkSameBitmapHC(Bitmap b1, Bitmap b2) {
    if (b1 == null || b2 == null) {
        return false;
    }
    return b1.sameAs(b2);
}
 
Example 12
Source File: PlayerPageFragment.java    From Android-Remote with GNU General Public License v3.0 4 votes vote down vote up
/**
 * The track changed. Update the metadata shown on the user interface
 */
@SuppressLint("NewApi")
public void updateTrackMetadata() {
    // Get the currently played song
    MySong currentSong = App.Clementine.getCurrentSong();
    if (currentSong == null) {
        // If none is played right now, show a text and the clementine icon
        mTvArtist.setText(getString(R.string.player_nosong));
        mTvTitle.setText("");
        mTvAlbum.setText("");

        mTvGenre.setText("");
        mTvYear.setText("");
        mTvLength.setText("");

        mSbPosition.setEnabled(false);

        mImgArt.setImageResource(R.drawable.icon_large);
    } else {
        mTvArtist.setText(currentSong.getArtist());
        mTvTitle.setText(currentSong.getTitle());
        mTvAlbum.setText(currentSong.getAlbum());

        mTvGenre.setText(currentSong.getGenre());
        mTvYear.setText(currentSong.getYear());

        // Check if a coverart is valid
        Bitmap newArt = currentSong.getArt();
        Bitmap oldArt = mCurrentSong.getArt();

        if (newArt == null) {
            mImgArt.setImageResource(R.drawable.icon_large);
        } else if (oldArt == null
                || !oldArt.sameAs(newArt)) {
            // Transit only if the cover changed
            if (mFirstCall) {
                mImgArt.setImageBitmap(newArt);
            } else {
                mImgArt.startAnimation(mAlphaDown);
            }
        }
        mCurrentSong = currentSong;
    }

    mFirstCall = false;
}
 
Example 13
Source File: ExoMetaLogActivity.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate(Bundle bundle) {
  super.onCreate(bundle);
  try {
    ApplicationInfo appInfo =
        getPackageManager().getApplicationInfo("buck.exotest", PackageManager.GET_META_DATA);

    Bitmap icon = getIcon(appInfo);
    Bitmap defaultIcon =
        ((BitmapDrawable) getPackageManager().getApplicationIcon(getApplicationInfo()))
            .getBitmap();
    if (icon == null) {
      Log.i("EXOPACKAGE_TEST_META", "Found no icon");
    } else if (icon.sameAs(defaultIcon)) {
      Log.i("EXOPACKAGE_TEST_META", "Found default icon");
    } else {
      Log.i("EXOPACKAGE_TEST_META", "META_ICON=" + icon.getWidth() + "_" + icon.getHeight());
    }
    String name = getName(appInfo);
    if (name == null) {
      Log.i("EXOPACKAGE_TEST_META", "Found no name");
    } else {
      Log.i("EXOPACKAGE_TEST_META", "META_NAME=" + name);
    }
    String[] meta = getMeta(appInfo);
    if (meta == null) {
      Log.i("EXOPACKAGE_TEST_META", "Found no metadata");
    } else {
      String metaStr = "<";
      for (int i = 0; i < meta.length; i++) {
        metaStr += (i == 0 ? "" : ",") + meta[i];
      }
      metaStr += ">";
      Log.i("EXOPACKAGE_TEST_META", "META_DATA=" + metaStr);
    }
  } catch (Exception e) {
    Log.i("EXOPACKAGE_TEST_META_DEBUG", "Got an exception", e);
  }
  Log.i("EXOPACKAGE_TEST_META", "FINISHED");
  finish();
}
 
Example 14
Source File: PixelByPixelPicturesComparator.java    From SmoothClicker with MIT License 3 votes vote down vote up
/**
 * Compares two pictures as bitmaps.
 * If the pictures are the same, returns true.
 * If the pictures differs, returns false.
 *
 * @param basePicture - The base picture
 * @param pickedPicture - The picked picture, a more recent one, to compare to the base picture
 * @return boolean - Returns true if pictures have the same dimensions, config, and pixel data as this bitmap ; false otherwise
 * @throws PicturesComparatorException - If a problem occurs during the comparison
 */
@Override
public boolean comparePictures( Bitmap basePicture, Bitmap pickedPicture ) throws PicturesComparatorException {
    if ( basePicture == null ) throw new PicturesComparatorException("The base picture is null");
    if ( pickedPicture == null ) throw new PicturesComparatorException("The picked picture is null");
    boolean areEqual = basePicture.sameAs(pickedPicture);
    Logger.d(LOG_TAG, "Pictures are equal: " + areEqual);
    return basePicture.sameAs(pickedPicture);
}
 
Example 15
Source File: NotificationUiHelper.java    From AcDisplay with GNU General Public License v2.0 2 votes vote down vote up
/**
 * @return {@code true} if both {@link Bitmap bitmaps} are {@code null}
 * or if the {@link Bitmap bitmaps} are equal according to
 * {@link android.graphics.Bitmap#sameAs(android.graphics.Bitmap)}, {@code false} otherwise.
 */
private boolean sameAs(@Nullable Bitmap bitmap, @Nullable Bitmap bitmap2) {
    return bitmap == bitmap2 || bitmap != null && bitmap2 != null && bitmap.sameAs(bitmap2);
}