Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.graphics;
     18 
     19 import com.android.layoutlib.bridge.impl.DelegateManager;
     20 import com.android.layoutlib.bridge.impl.PorterDuffUtility;
     21 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
     22 
     23 import android.graphics.PorterDuff.Mode;
     24 
     25 import java.awt.Composite;
     26 
     27 import static com.android.layoutlib.bridge.impl.PorterDuffUtility.getPorterDuffMode;
     28 
     29 /**
     30  * Delegate implementing the native methods of android.graphics.PorterDuffXfermode
     31  *
     32  * Through the layoutlib_create tool, the original native methods of PorterDuffXfermode have been
     33  * replaced by calls to methods of the same name in this delegate class.
     34  *
     35  * This class behaves like the original native implementation, but in Java, keeping previously
     36  * native data into its own objects and mapping them to int that are sent back and forth between
     37  * it and the original PorterDuffXfermode class.
     38  *
     39  * Because this extends {@link Xfermode_Delegate}, there's no need to use a
     40  * {@link DelegateManager}, as all the PathEffect classes will be added to the manager owned by
     41  * {@link Xfermode_Delegate}.
     42  *
     43  */
     44 public class PorterDuffXfermode_Delegate extends Xfermode_Delegate {
     45 
     46     // ---- delegate data ----
     47 
     48     private final Mode mMode;
     49 
     50     // ---- Public Helper methods ----
     51 
     52     public Mode getMode() {
     53         return mMode;
     54     }
     55 
     56     @Override
     57     public Composite getComposite(int alpha) {
     58         return PorterDuffUtility.getComposite(mMode, alpha);
     59     }
     60 
     61     @Override
     62     public boolean isSupported() {
     63         return true;
     64     }
     65 
     66     @Override
     67     public String getSupportMessage() {
     68         // no message since isSupported returns true;
     69         return null;
     70     }
     71 
     72 
     73     // ---- native methods ----
     74 
     75     @LayoutlibDelegate
     76     /*package*/ static long nativeCreateXfermode(int mode) {
     77         PorterDuffXfermode_Delegate newDelegate = new PorterDuffXfermode_Delegate(mode);
     78         return sManager.addNewDelegate(newDelegate);
     79     }
     80 
     81     // ---- Private delegate/helper methods ----
     82 
     83     private PorterDuffXfermode_Delegate(int mode) {
     84         mMode = getPorterDuffMode(mode);
     85     }
     86 
     87 }
     88