2017-09-02 33 views
0

我想通過使用Xamarin.Android擴展Android核心ImageView類來創建自定義ImageView類。下面是Java中的一段代碼和C#中不完整的實現。我需要最後2種方法的幫助。將Android Custom ImageView轉換爲Xamarin.Android

ANDROID Java代碼

import android.content.Context; 
import android.content.res.ColorStateList; 
import android.content.res.TypedArray; 
import android.util.AttributeSet; 
import android.widget.ImageView; 

public class IconViewView extends ImageView { 

private ColorStateList tint; 

public IconView(Context context) { 
    super(context); 
} 

public IconView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context, attrs, 0); 
} 

public IconView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(context, attrs, defStyle); 
} 

private void init(Context context, AttributeSet attrs, int defStyle) { 
    TypedArray a = context.obtainStyledAttributes(
    attrs, R.styleable.IconView, defStyle, 0); 
    tint = a.getColorStateList(R.styleable.IconView_iconTint); 
    a.recycle(); 
} 

@Override 
protected void drawableStateChanged() { 
    super.drawableStateChanged(); 
    if (tint != null && tint.isStateful()) { 
    updateTintColor(); 
    } 
} 

public void setColorFilter(ColorStateList tint) { 
    this.tint = tint; 
    super.setColorFilter(tint.getColorForState(getDrawableState(), 0)); 
} 

private void updateTintColor() { 
    int color = tint.getColorForState(getDrawableState(), 0); 
    setColorFilter(color); 
} 

} 

ANDROID XAMARIN C#代碼

using Android.Content; 
using Android.Content.Res; 
using Android.Graphics; 
using Android.Support.V4.Content; 
using Android.Support.V4.Graphics.Drawable; 
using Android.Util; 
using Android.Widget; 

namespace Example.Droid.App.Views 
{ 
    public class IconView : ImageView 
    { 
     private ColorStateList tint; 
     private Context context; 
     public IconView(Context context) :base(context) 
    { 
     Initialize(context, null, 0); 
    } 

    public IconView(Context context, IAttributeSet attrs) : 
     base(context, attrs) 
    { 
     Initialize(context, attrs, 0); 
    } 

    public IconView(Context context, IAttributeSet attrs, int defStyle) : 
     base(context, attrs, defStyle) 
    { 
     Initialize(context, attrs, defStyle); 
    } 

    void Initialize(Context mContext, IAttributeSet attrs, int defStyle) 
    { 
     context = mContext; 
     TypedArray a = context.ObtainStyledAttributes(attrs, Resource.Styleable.IconView, defStyle, 0); 
     tint = a.GetColorStateList(Resource.Styleable.IconView_iconTint); 
     a.Recycle(); 
    } 

    protected override void DrawableStateChanged() 
    { 
     base.DrawableStateChanged(); 
     if (tint != null && tint.IsStateful) 
      UpdateTintColor(); 
    } 

    private void UpdateTintColor() { 
     /* I NEED HELP HERE */ 
    } 
    public void SetColorFilter(ColorStateList tint) { 
     /* I NEED HELP HERE */ 
    } 

    } 
} 

我需要一些幫助與Xamarin C#

private void UpdateTintColor() { 
    /* I NEED HELP HERE */ 
} 
public void SetColorFilter(ColorStateList tint) { 
    /* I NEED HELP HERE */ 
} 
+0

您需要將「override」關鍵字添加到任何重寫的方法,並且您可以通常使用Java使用'super'的C#'base'關鍵字。 – Jason

+0

謝謝傑森。這裏有什麼。'私人無效UpdateTintColor(){ /*我需要幫助HERE * /} 公共 無效SetColorFilter(ColorStateList色調){ /*我需要幫助HERE */ }' – Bismarck

回答

2

使用這些方法new Color(),並且無論何時需要將int轉換爲Color,它都會超載。

https://developer.xamarin.com/api/constructor/Android.Graphics.Color.Color/p/System.Int32/

private void UpdateTintColor() 
{ 
    var color = new Color(tint.GetColorForState(GetDrawableState(), new Color(0))); 
    SetColorFilter(color); 
} 
public void SetColorFilter(ColorStateList tint) 
{ 
    this.tint = tint; 
    base.SetColorFilter(new Color(tint.GetColorForState(GetDrawableState(), new Color(0)))); 
} 

有關於這個問題在這裏多優秀的錯誤:

https://bugzilla.xamarin.com/show_bug.cgi?id=36396#c3 https://bugzilla.xamarin.com/show_bug.cgi?id=57521

請抄送自己在這個問題上並添加註釋來形容難度使用此API沒有擴展方法,我們將努力改進這些。

+0

謝謝你這麼多。太棒了。 – Bismarck

0

最後我得到它的工作。我擴展了AppCompatImageView,並感謝Jon Douglas。這裏是我的代碼

1.應用程序/查看/ IconView.cs

using System; 
using Android.Content; 
using Android.Content.Res; 
using Android.Graphics; 
using Android.Support.V4.Content; 
using Android.Support.V4.Graphics.Drawable; 
using Android.Support.V7.Widget; 
using Android.Util; 
using Android.Widget; 

namespace Example.Droid.App.Views 
{ 
    public class IconView : AppCompatImageView 
    { 
     private ColorStateList tint; 
     private Context context; 
     public IconView(Context context) :base(context) 
    { 
     Initialize(context, null, 0); 
    } 

    public IconView(Context context, IAttributeSet attrs) : 
     base(context, attrs) 
    { 
     Initialize(context, attrs, 0); 
    } 

    public IconView(Context context, IAttributeSet attrs, int defStyle) : 
     base(context, attrs, defStyle) 
    { 
     Initialize(context, attrs, defStyle); 
    } 

    void Initialize(Context mContext, IAttributeSet attrs, int defStyle) 
    { 
     context = mContext; 
     TypedArray a = context.ObtainStyledAttributes(attrs, Resource.Styleable.IconView, defStyle, 0); 
     tint = a.GetColorStateList(Resource.Styleable.IconView_iconTint); 
     a.Recycle(); 
    } 

    protected override void DrawableStateChanged() 
    { 
     base.DrawableStateChanged(); 
     if (tint != null && tint.IsStateful) 
      UpdateTintColor(); 
    } 

    private void UpdateTintColor() 
    { 
     var color = new Color(tint.GetColorForState(GetDrawableState(), new Color(0))); 
     SetColorFilter(color); 
    } 

    public void SetColorFilter(ColorStateList tint) 
    { 
     this.tint = tint; 
     base.SetColorFilter(new Color(tint.GetColorForState(GetDrawableState(), new Color(0)))); 
    } 

    } 
} 

2.資源/佈局零件/ activity_example.axml

<Example.Droid.App.Views.IconView 
android:layout_width="30dp" 
android:layout_height="30dp" 
app:srcCompat="@drawable/ic_camera" 
android:layout_margin="5dp" 
android:duplicateParentState="true" 
app:iconTint="@color/primary_selector"/> 

3.資源/color/primary_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
     android:color="#FFFFFF"/> <!-- pressed --> 
    <item android:state_selected="true" 
     android:state_focused="true" 
     android:color="#FFFFFF"/> <!-- focused --> 
    <item android:color="#CCCCCC"/> <!-- default --> 
</selector> 

4.資源/價值/ attrs.xml

<?xml version="1.0" encoding="UTF-8" ?> 
    <resources> 
    <declare-styleable name="IconView"> 
    <attr name="iconTint" format="reference|color" /> 
    </declare-styleable> 
    </resources> 

5.資源/繪製/ ic_camera.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<vector xmlns:android="http://schemas.android.com/apk/res/android" 
android:height="24dp" 
android:width="24dp" 
android:viewportWidth="24" 
android:viewportHeight="24"> 
<path android:fillColor="#000" android:pathData="M4,4H7L9,2H15L17,4H20A2,2 0 0,1 22,6V18A2,2 0 0,1 20,20H4A2,2 0 0,1 2,18V6A2,2 0 0,1 4,4M12,7A5,5 0 0,0 7,12A5,5 0 0,0 12,17A5,5 0 0,0 17,12A5,5 0 0,0 12,7M12,9A3,3 0 0,1 15,12A3,3 0 0,1 12,15A3,3 0 0,1 9,12A3,3 0 0,1 12,9Z" /> 

乾杯......

相關問題