【发布时间】:2017-09-02 16:35:19
【问题描述】:
我正在尝试通过使用 Xamarin.Android 扩展 Android 核心 ImageView 类来创建自定义 ImageView 类。下面是一段 Java 代码和一个不完整的 C# 实现。我需要最后两种方法的帮助。
安卓 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 */
}
【问题讨论】:
-
任何被覆盖的方法都需要添加
override关键字,一般可以在Java使用super的地方使用C#的base关键字。 -
谢谢杰森。这里有什么。
private void UpdateTintColor() { /* I NEED HELP HERE */ } public void SetColorFilter(ColorStateList tint) { /* I NEED HELP HERE */ }