欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

【Android 开发】使用不同的 interpolator、色彩渐变

程序员文章站 2023-09-06 21:19:28
singlefragmentactivity.java package com.example.aster.draganddraw; import android.os.bundle;...

singlefragmentactivity.java

package com.example.aster.draganddraw;

import android.os.bundle;
import android.support.annotation.layoutres;
import android.support.v4.app.fragment;
import android.support.v4.app.fragmentmanager;
import android.support.v7.app.appcompatactivity;

public abstract class singlefragmentactivity extends appcompatactivity {

 protected abstract fragment createfragment();

 @layoutres
 protected int getlayoutresid() {
  return r.layout.activity_fragment;
 }

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(getlayoutresid());

  fragmentmanager fm = getsupportfragmentmanager();
  fragment fragment = fm.findfragmentbyid(r.id.fragment_container);

  if (fragment == null) {
fragment = createfragment();
fm.begintransaction().add(r.id.fragment_container, fragment).commit();
  }
 }
}

sunsetactivity.java

package com.example.aster.sunset;

import android.support.v4.app.fragment;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;

public class sunsetactivity extends singlefragmentactivity {

 @override
 protected fragment createfragment() {
  return sunsetfragment.newinstance();
 }
}

sunsetfragment.java

package com.example.aster.sunset;

import android.animation.argbevaluator;
import android.animation.objectanimator;
import android.content.res.resources;
import android.os.bundle;
import android.support.v4.app.fragment;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.view.animation.accelerateinterpolator;

public class sunsetfragment extends fragment {

 private view msceneview;
 private view msunview;
 private view mskyview;

 private int mblueskycolor;
 private int msunsetskycolor;
 private int mnightskycolor;

 public static sunsetfragment newinstance() {
  return new sunsetfragment();
 }

 @override
 public view oncreateview(layoutinflater inflater, viewgroup container,
  bundle savedinstancestate) {
  view view = inflater.inflate(r.layout.fragment_sunset, container, false);

  msceneview = view;
  msunview = view.findviewbyid(r.id.sun);
  mskyview = view.findviewbyid(r.id.sky);

  resources resources = getresources();
  mblueskycolor = resources.getcolor(r.color.blue_sky);
  msunsetskycolor = resources.getcolor(r.color.sunset_sky);
  mnightskycolor = resources.getcolor(r.color.night_sky);

  msceneview.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view v) {
 startanimation();
}
  });

  return view;
 }

 private void startanimation() {
  float sunystart = msunview.gettop();
  float sunyend = mskyview.getheight();

  objectanimator heightanimator = objectanimator
 .offloat(msunview, "y", sunystart, sunyend)
 .setduration(3000);
  heightanimator.setinterpolator(new accelerateinterpolator());

  objectanimator sunsetskyanimator = objectanimator
 .ofint(mskyview, "backgroundcolor", mblueskycolor, msunsetskycolor)
 .setduration(3000);
  sunsetskyanimator.setevaluator(new argbevaluator());

  heightanimator.start();
  sunsetskyanimator.start();
 }
}

res/drawable/sun.xml



 

activity_fragment.xml


<framelayout android:id="@+id/fragment_container" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="https://schemas.android.com/apk/res/android"></framelayout>

fragment_sunset.xml



 <framelayout android:background="@color/blue_sky" android:id="@+id/sky" android:layout_height="0dp" android:layout_weight="0.61" android:layout_width="match_parent">
  
 </framelayout>

 

res/values/colors.xml



 #3f51b5
 #303f9f
 #ff4081

 #fcfcb7
 #1e7ac7
 #ec8100
 #05192e
 #224869

【Android 开发】使用不同的 interpolator、色彩渐变

【Android 开发】使用不同的 interpolator、色彩渐变

【Android 开发】使用不同的 interpolator、色彩渐变

【Android 开发】使用不同的 interpolator、色彩渐变