下面列出了怎么用android.graphics.drawable.TransitionDrawable的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (BuildConfig.DEBUG) {
Log.i("CATEGORY", "AHHHHH!!!");
}
final ImageView image = (ImageView) findViewById(R.id.image);
final ToggleButton button = (ToggleButton) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
TransitionDrawable drawable = (TransitionDrawable) image
.getDrawable();
if (button.isChecked()) {
drawable.startTransition(1000);
} else {
drawable.reverseTransition(1000);
}
}
});
}
/**
* Called when the processing is complete and the final drawable should be
* set on the ImageView.
*
* @param imageView
* @param drawable
*/
private void setImageDrawable(ImageView imageView, Drawable drawable) {
if (mFadeInBitmap) {
// Transition drawable with a transparent drawable and the final drawable
final TransitionDrawable td =
new TransitionDrawable(new Drawable[] {
new ColorDrawable(android.R.color.transparent),
drawable
});
// Set background to loading bitmap
imageView.setBackgroundDrawable(
new BitmapDrawable(mResources, mLoadingBitmap));
imageView.setImageDrawable(td);
td.startTransition(FADE_IN_TIME);
} else {
imageView.setImageDrawable(drawable);
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
// test transition
View v = findViewById(R.id.test_transition);
TransitionDrawable drawable = (TransitionDrawable) v.getBackground();
drawable.startTransition(1000);
// test scale
View testScale = findViewById(R.id.test_scale);
ScaleDrawable testScaleDrawable = (ScaleDrawable) testScale.getBackground();
testScaleDrawable.setLevel(10);
// test clip
ImageView testClip = (ImageView) findViewById(R.id.test_clip);
ClipDrawable testClipDrawable = (ClipDrawable) testClip.getDrawable();
testClipDrawable.setLevel(8000);
// test custom drawable
View testCustomDrawable = findViewById(R.id.test_custom_drawable);
CustomDrawable customDrawable = new CustomDrawable(Color.parseColor("#0ac39e"));
testCustomDrawable.setBackgroundDrawable(customDrawable);
}
}
private void setStarredIcon() {
if (starItem == null) return;
final TransitionDrawable mFabBackground = (TransitionDrawable) mFAB.getBackground();
if (isStarred) {
starItem.setIcon(R.drawable.ic_action_star_white);
} else {
starItem.setIcon(R.drawable.ic_action_star_outline_white);
}
mFAB.animate().scaleX(0).setDuration(160);
mFAB.animate().scaleY(0);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (isStarred) {
mFAB.setImageResource(R.drawable.ic_action_star_white);
mFabBackground.startTransition((int) mFAB.animate().getDuration());
} else {
mFAB.setImageResource(R.drawable.ic_action_copy);
mFabBackground.resetTransition();
}
mFAB.animate().scaleX(1);
mFAB.animate().scaleY(1);
}
}, 220);
}
@SuppressWarnings("deprecation")
@Override
public void setImageBitmap(Bitmap bm) {
Context context = getContext();
if (context != null) {
if (bm == null) {
if (defaultShown && defaultImageCounter > 0)
defaultImageCounter--;
bm = ((BitmapDrawable) getResources().getDrawable(this.defaultImageResources[defaultImageCounter++ % defaultImageResources.length])).getBitmap();
defaultShown = true;
} else
defaultShown = false;
Resources resources = context.getResources();
TransitionDrawable td = new TransitionDrawable(new Drawable[]{
new ColorDrawable(resources.getColor(android.R.color.transparent)),
new BitmapDrawable(context.getResources(), bm)
});
setImageDrawable(td);
td.startTransition(FADE_IN_TIME_MS);
}
}
public void changeActionBarColor(int newColor) {
int color = newColor != 0 ? newColor : SettingsActivity.getPrimaryColor(this);
Drawable colorDrawable = new ColorDrawable(color);
if (oldBackground == null) {
getSupportActionBar().setBackgroundDrawable(colorDrawable);
} else {
TransitionDrawable td = new TransitionDrawable(new Drawable[] { oldBackground, colorDrawable });
getSupportActionBar().setBackgroundDrawable(td);
td.startTransition(200);
}
oldBackground = colorDrawable;
}
private void changeActionBarColor() {
int color = SettingsActivity.getPrimaryColor(this);
Drawable colorDrawable = new ColorDrawable(color);
if (oldBackground == null) {
getSupportActionBar().setBackgroundDrawable(colorDrawable);
} else {
TransitionDrawable td = new TransitionDrawable(new Drawable[] { oldBackground, colorDrawable });
getSupportActionBar().setBackgroundDrawable(td);
td.startTransition(200);
}
oldBackground = colorDrawable;
setUpStatusBar();
}
public void changeActionBarColor(int newColor) {
int color = newColor != 0 ? newColor : SettingsActivity.getPrimaryColor(this);
Drawable colorDrawable = new ColorDrawable(color);
if (oldBackground == null) {
getSupportActionBar().setBackgroundDrawable(colorDrawable);
} else {
TransitionDrawable td = new TransitionDrawable(new Drawable[] { oldBackground, colorDrawable });
getSupportActionBar().setBackgroundDrawable(td);
td.startTransition(200);
}
oldBackground = colorDrawable;
}
private void changeActionBarColor() {
int color = SettingsActivity.getPrimaryColor(this);
Drawable colorDrawable = new ColorDrawable(color);
if (oldBackground == null) {
getSupportActionBar().setBackgroundDrawable(colorDrawable);
} else {
TransitionDrawable td = new TransitionDrawable(new Drawable[] { oldBackground, colorDrawable });
getSupportActionBar().setBackgroundDrawable(td);
td.startTransition(200);
}
oldBackground = colorDrawable;
setUpStatusBar();
}
/**
* Start a transition to the new image
*
* @param drawable the drawable to set
*/
public void setFadingImageDrawable(Drawable drawable) {
Drawable currentDrawable = getDrawable();
if ((currentDrawable != null) && (currentDrawable instanceof TransitionDrawable)) {
currentDrawable = ((TransitionDrawable) currentDrawable).getDrawable(1);
}
if (currentDrawable != null) {
Drawable[] arrayDrawable = new Drawable[2];
arrayDrawable[0] = currentDrawable;
arrayDrawable[1] = drawable;
TransitionDrawable transitionDrawable = new TransitionDrawable(arrayDrawable);
transitionDrawable.setCrossFadeEnabled(true);
setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(transitionDurationMillis);
} else {
setImageDrawable(drawable);
}
}
@Override
protected Drawable create(final Point page) {
// place bugdroid as the background at row 2, column 1
if (page.y == 2 && page.x == 1) {
int resid = R.drawable.bugdroid_large;
new DrawableLoadingTask(mContext) {
@Override
protected void onPostExecute(Drawable result) {
TransitionDrawable background = new TransitionDrawable(new Drawable[] {
mClearBg,
result
});
mPageBackgrounds.put(page, background);
notifyPageBackgroundChanged(page.y, page.x);
background.startTransition(TRANSITION_DURATION_MILLIS);
}
}.execute(resid);
}
return GridPagerAdapter.BACKGROUND_NONE;
}
/**
* Called when the processing is complete and the final bitmap should be set on the ImageView.
*
* @param imageView The ImageView to set the bitmap to.
* @param bitmap The new bitmap to set.
*/
private void setImageBitmap(ImageView imageView, Bitmap bitmap) {
if (mFadeInBitmap) {
// Transition drawable to fade from loading bitmap to final bitmap
final TransitionDrawable td =
new TransitionDrawable(new Drawable[]{
new ColorDrawable(android.R.color.transparent),
new BitmapDrawable(mResources, bitmap)
});
imageView.setBackgroundDrawable(imageView.getDrawable());
imageView.setImageDrawable(td);
td.startTransition(FADE_IN_TIME);
} else {
imageView.setImageBitmap(bitmap);
}
}
@Override
protected void onPostExecute(Drawable result) {
if (result != null) {
if (mBlurredArt.getDrawable() != null) {
final TransitionDrawable td =
new TransitionDrawable(new Drawable[]{
mBlurredArt.getDrawable(),
result
});
mBlurredArt.setImageDrawable(td);
td.startTransition(200);
} else {
mBlurredArt.setImageDrawable(result);
}
}
}
@Override
protected void onPostExecute(Drawable result) {
if (result != null) {
if (mBlurredArt.getDrawable() != null) {
final TransitionDrawable td =
new TransitionDrawable(new Drawable[]{
mBlurredArt.getDrawable(),
result
});
mBlurredArt.setImageDrawable(td);
td.startTransition(200);
} else {
mBlurredArt.setImageDrawable(result);
}
}
}
private void fadeThumbnailIn(SnippetArticle snippet, Bitmap thumbnail) {
mImageCallback = null;
if (thumbnail == null) return; // Nothing to do, we keep the placeholder.
// We need to crop and scale the downloaded bitmap, as the ImageView we set it on won't be
// able to do so when using a TransitionDrawable (as opposed to the straight bitmap).
// That's a limitation of TransitionDrawable, which doesn't handle layers of varying sizes.
Resources res = mThumbnailView.getResources();
int targetSize = res.getDimensionPixelSize(R.dimen.snippets_thumbnail_size);
Bitmap scaledThumbnail = ThumbnailUtils.extractThumbnail(
thumbnail, targetSize, targetSize, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
// Store the bitmap to skip the download task next time we display this snippet.
snippet.setThumbnailBitmap(scaledThumbnail);
// Cross-fade between the placeholder and the thumbnail.
Drawable[] layers = {mThumbnailView.getDrawable(),
new BitmapDrawable(mThumbnailView.getResources(), scaledThumbnail)};
TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
mThumbnailView.setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(FADE_IN_ANIMATION_TIME_MS);
}
@Override
public void setImageBitmap(Bitmap bitmap) {
// For configureBounds to be called and quick set.
super.setImageBitmap(bitmap);
if (bitmap != null) {
Resources res = getResources();
Drawable[] layers = new Drawable[2];
// Layer 0.
layers[0] = res.getDrawable(R.drawable.default_photo);
// Layer 1.
// For masking the Bitmap after scale_type is used.
layers[1] = new BitmapDrawable(res, clipBitmap(bitmap));
TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
transitionDrawable.setCrossFadeEnabled(true);
setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(400);
}
}
/**
* Called when the processing is complete and the final bitmap should be set on the ImageView.
*
* @param imageView The ImageView to set the bitmap to.
* @param bitmap The new bitmap to set.
*/
private void setImageBitmap(CircleImageView imageView, Bitmap bitmap) {
if (mFadeInBitmap) {
// Transition drawable to fade from loading bitmap to final bitmap
final TransitionDrawable td =
new TransitionDrawable(new Drawable[]{
new ColorDrawable(android.R.color.transparent),
new BitmapDrawable(mResources, bitmap)
});
//imageView.setBackgroundDrawable(imageView.getDrawable());
imageView.setImageDrawable(td);
td.startTransition(FADE_IN_TIME);
} else {
imageView.setImageBitmap(bitmap);
}
}
@Override public void setupAppcAppView() {
TypedValue value = new TypedValue();
this.getContext()
.getTheme()
.resolveAttribute(R.attr.appview_toolbar_bg_appc, value, true);
int drawableId = value.resourceId;
TransitionDrawable transition =
(TransitionDrawable) ContextCompat.getDrawable(getContext(), drawableId);
collapsingToolbarLayout.setBackgroundDrawable(transition);
transition.startTransition(APPC_TRANSITION_MS);
AlphaAnimation animation1 = new AlphaAnimation(0f, 1.0f);
animation1.setDuration(APPC_TRANSITION_MS);
collapsingAppcBackground.setAlpha(1f);
collapsingAppcBackground.setVisibility(View.VISIBLE);
collapsingAppcBackground.startAnimation(animation1);
install.setBackgroundDrawable(getContext().getResources()
.getDrawable(R.drawable.appc_gradient_rounded));
downloadProgressBar.setProgressDrawable(
ContextCompat.getDrawable(getContext(), R.drawable.appc_progress));
flagThisAppSection.setVisibility(View.GONE);
}
@Test
public void testImageFadeDrawable() {
ColorDrawable defaultDrawable = new ColorDrawable(Color.BLUE);
CrossbowImage.Builder builder = new CrossbowImage.Builder(RuntimeEnvironment.application, null, null);
builder.placeholder(defaultDrawable);
builder.scale(ImageView.ScaleType.CENTER);
builder.fade(200);
CrossbowImage crossbowImage = builder.into(imageView).load();
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ALPHA_8);
crossbowImage.setBitmap(bitmap, false);
TransitionDrawable drawable = (TransitionDrawable) imageView.getDrawable();
assertTrue(drawable.getNumberOfLayers() == 2);
}
private void setImage(ImageView imageView, Bitmap bitmap) {
if (animate) {
Resources resources = imageView.getResources();
BitmapDrawable drawable = new BitmapDrawable(resources, bitmap);
Drawable currentDrawable = imageView.getDrawable();
if (currentDrawable != null) {
Drawable[] arrayDrawable = new Drawable[2];
arrayDrawable[0] = currentDrawable;
arrayDrawable[1] = drawable;
final TransitionDrawable transitionDrawable = new TransitionDrawable(arrayDrawable);
transitionDrawable.setCrossFadeEnabled(true);
imageView.setImageDrawable(transitionDrawable);
transitionDrawable.startTransition(150);
} else {
imageView.setImageDrawable(drawable);
}
} else {
imageView.setImageBitmap(bitmap);
}
}
/**
* Called when the processing is complete and the final drawable should be
* set on the ImageView.
*
* @param imageView
* @param drawable
*/
private void setImageDrawable(ImageView imageView, Drawable drawable) {
if (mFadeInBitmap) {
// Transition drawable with a transparent drawable and the final drawable
final TransitionDrawable td =
new TransitionDrawable(new Drawable[] {
new ColorDrawable(android.R.color.transparent),
drawable
});
// Set background to loading bitmap
imageView.setBackgroundDrawable(
new BitmapDrawable(mResources, mLoadingBitmap));
imageView.setImageDrawable(td);
td.startTransition(FADE_IN_TIME);
} else {
imageView.setImageDrawable(drawable);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("");
but_flash = (ImageButton) findViewById(R.id.but_flash_pto);
sw_func_toggle = (SwitchCompat) findViewById(R.id.sw_func_toggle);
transAnimButFlash = (TransitionDrawable) but_flash.getBackground();
transAnimButFlash.resetTransition();
if (SettingsUtils.isFirstTime(this)) {
this.showDialogWelcome();
}
}
/**
* Called when the processing is complete and the final drawable should be
* set on the ImageView.
*
* @param imageView
* @param drawable
*/
private void setImageDrawable(ImageView imageView, Drawable drawable) {
if (mFadeInBitmap) {
// Transition drawable with a transparent drawable and the final drawable
final TransitionDrawable td =
new TransitionDrawable(new Drawable[] {
new ColorDrawable(android.R.color.transparent),
drawable
});
// Set background to loading bitmap
imageView.setBackgroundDrawable(
new BitmapDrawable(mResources, mLoadingBitmap));
imageView.setImageDrawable(td);
td.startTransition(FADE_IN_TIME);
} else {
imageView.setImageDrawable(drawable);
}
}
/**
* Called when the processing is complete and the final drawable should be
* set on the ImageView.
*
* @param imageView
* @param drawable
*/
private void setImageDrawable(ImageView imageView, Drawable drawable) {
if (mFadeInBitmap) {
// Transition drawable with a transparent drawable and the final drawable
final TransitionDrawable td =
new TransitionDrawable(new Drawable[]{
new ColorDrawable(android.R.color.transparent),
drawable
});
// Set background to loading bitmap
imageView.setBackgroundDrawable(
new BitmapDrawable(mResources, mLoadingBitmap));
imageView.setImageDrawable(td);
td.startTransition(FADE_IN_TIME);
} else {
imageView.setImageDrawable(drawable);
}
}
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView image = (ImageView) findViewById(R.id.image);
final ToggleButton button = (ToggleButton) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
TransitionDrawable drawable = (TransitionDrawable) image
.getDrawable();
if (button.isChecked()) {
drawable.startTransition(1000);
} else {
drawable.reverseTransition(1000);
}
}
});
}
/**
* Animates from the previous drawable to the current drawable in one of two ways.
*
* <ol>
* <li>Using the default animation provided in the constructor if the previous drawable is null</li>
* <li>Using the cross fade animation with the duration provided in the constructor if the previous
* drawable is non null</li>
* </ol>
*
* @param current {@inheritDoc}
* @param adapter {@inheritDoc}
* @return {@inheritDoc}
*/
@Override
public boolean animate(T current, ViewAdapter adapter) {
Drawable previous = adapter.getCurrentDrawable();
if (previous != null) {
TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[] { previous, current });
transitionDrawable.setCrossFadeEnabled(true);
transitionDrawable.startTransition(duration);
adapter.setDrawable(transitionDrawable);
return true;
} else {
defaultAnimation.animate(current, adapter);
return false;
}
}
public static TransitionDrawable createTransitionDrawable(Drawable start, Drawable end) {
final Drawable[] drawables = new Drawable[2];
drawables[0] = start;
drawables[1] = end;
return new TransitionDrawable(drawables);
}
@Override
public void display(@NonNull SketchView sketchView, @NonNull Drawable newDrawable) {
TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[]{new ColorDrawable(color), newDrawable});
sketchView.clearAnimation();
sketchView.setImageDrawable(transitionDrawable);
transitionDrawable.setCrossFadeEnabled(true);
transitionDrawable.startTransition(duration);
}
public CommentsGridPagerAdapter(Context context, FragmentManager fm,
ArrayList<Comment> comments) {
super(fm);
mContext = context;
mRows = new ArrayList<>();
mPageBackgrounds = new LruCache<Point, Drawable>(1) {
@Override protected Drawable create(final Point page) {
TransitionDrawable background = new TransitionDrawable(new Drawable[]{new ColorDrawable(
mContext.getResources().getColor(R.color.primary)), new ColorDrawable(
mContext.getResources().getColor(R.color.primary_dark))});
mPageBackgrounds.put(page, background);
notifyPageBackgroundChanged(page.y, page.x);
background.startTransition(TRANSITION_DURATION_MILLIS);
return background;
}
};
for (Comment comment : comments) {
Fragment cardFragment = cardFragment(comment);
if (comment.getReplies() != null && !comment.getReplies().isEmpty()) {
Fragment actionFragment = ActionFragment.create(comment.getReplies());
mRows.add(new Row(cardFragment, actionFragment));
} else {
mRows.add(new Row(cardFragment));
}
}
}
private void removeSelection(View pressedChild, MotionEvent event)
{
if (pressedChild == null)
{
return;
}
if (pressedChild != null && pressedChild.isEnabled())
{
positionSelector(currentChildPosition, pressedChild);
if (selectorDrawable != null)
{
Drawable d = selectorDrawable.getCurrent();
if (d != null && d instanceof TransitionDrawable)
{
((TransitionDrawable) d).resetTransition();
}
if (event != null && Build.VERSION.SDK_INT >= 21)
{
selectorDrawable.setHotspot(event.getX(), event.getY());
}
}
}
else
{
selectorRect.setEmpty();
}
updateSelectorState();
}