在自定义视图周围环绕文本

时间:2016-04-17 14:28:10

标签: java android android-custom-view android-glide

我创建了一个带有两个可绘制图像的自定义视图;第一个图像显示在屏幕顶部的整个宽度上,相应地调整了纵横比。我的第二张图像位于左侧顶部图像的正下方。

现在有一种方法可以在必要时有效地显示第二个图像包装右侧的文本。我的自定义视图如下:

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.ViewTarget;

import java.util.Random;

import demo.example.com.customarrayadapter.R;

import static android.support.v4.content.res.ResourcesCompat.getDrawable;

public class AspectRatioImageView extends TextView {
    ImageView ivTop, ivLower;
    ViewTarget viewTarget;
    private ViewTarget<AspectRatioImageView, GlideDrawable> viewTarget0,
        viewTarget1, viewTarget2;
    AspectRatioImageView customView0, customView1;
    private static Drawable mDrawableLeft, mDrawableRight;
    private Bitmap mBitmap0,mBitmap1;
    private static final int mColumnCount = 2;
    private static int mCount = 0;
    private int mLheight, mRheight;

public void initialize( final Context context) {
    Glide.
            with(context)
    .load("http://image.tmdb.org/t/p/w780//uS1SkjVviraGfFNgkDwe7ohTm8B.jpg")
            .asBitmap()
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
                    // Do something with bitmap here.
                    setLeftDrawable(bitmap);
                }
            });
    Glide.
            with(context)
            //.load("http://image.tmdb.org/t/p/w780//imSjsW6QRkH7fvhnqhQgjnbBBtd.jpg")
            .load("http://image.tmdb.org/t/p/w342//uS1SkjVviraGfFNgkDwe7ohTm8B.jpg")

            .asBitmap()
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
                    // Do something with bitmap here.
                    setRightDrawable(bitmap);
                }
            });
}

public void setLeftDrawable(Bitmap bitmap){
    mDrawableLeft = new BitmapDrawable(getResources(), bitmap);
    updateContentBounds();
    invalidate();
}

public void setRightDrawable(Bitmap bitmap){
    mDrawableRight = new BitmapDrawable(getResources(), bitmap);
    updateContentBounds();
    invalidate();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    if (w != oldw || h != oldh) {
        updateContentBounds();
    }
}

public AspectRatioImageView(Context context) {
    this(context, null);
    //if (!isInEditMode())
        initialize(context);
}

public AspectRatioImageView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    //if (!isInEditMode())
        initialize(context);
}

public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    //if (!isInEditMode())
        initialize(context);
}

public int getCustomWidth(){
    return getWidth();
}

public int getCustomHeight(){
    return (mLheight + mRheight);
}

public void updateContentBounds(){
    if (mDrawableLeft != null) {
        double ratio = (double) mDrawableLeft.getIntrinsicHeight() / mDrawableLeft.getIntrinsicWidth();
        int left = 0;
        int top = 0;
        int w = left + getWidth();
        int h = (int) (top + (getWidth() * ratio));
        mLheight = h;
        //int h = (int) (top + (mDrawable0.getIntrinsicHeight()) / 2);
        mDrawableLeft.setColorFilter(0xffff0000, PorterDuff.Mode.LIGHTEN);
        mDrawableLeft.setBounds(left, top,
                w, h);
    }

    if (mDrawableRight != null) {
        double ratio = (double) mDrawableRight.getIntrinsicHeight() / mDrawableRight.getIntrinsicWidth();
        int wleft = 0;
        int wtop = mLheight;
        int ww = wleft + getWidth() / 2;
        int wh = (int) (wtop + ((getWidth() / 2)*ratio));
        mRheight = (int) ((getWidth() / 2)*ratio);
        mDrawableRight.setColorFilter(0xdfef0000, PorterDuff.Mode.MULTIPLY);
        mDrawableRight.setBounds(wleft, wtop,
                ww, wh);
    }
    if ((mDrawableLeft != null) && (mDrawableRight != null))
        setCompoundDrawables(mDrawableLeft, null, mDrawableRight, null);
}

@Override
protected void onDraw(Canvas canvas) {
    //super.onDraw(canvas);
    // Log.d("LOG", " ***********onDraw()");
    if (mDrawableLeft != null) {
        mDrawableLeft.draw(canvas);
    }
    if (mDrawableRight != null) {
        mDrawableRight.draw(canvas);
    }
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    updateContentBounds();
    Log.d("LOG"," ****width:"+getCustomWidth()+" ****height:"+getCustomHeight());
    Drawable bg = mDrawableRight;
    if (bg != null) {
        //Get the width measurement
        int widthSize = View.resolveSize(getCustomWidth(), widthMeasureSpec);

        //Get the height measurement
        //int height = widthSize * bg.getIntrinsicHeight()/bg.getIntrinsicWidth();
        int heightSize = View.resolveSize(getCustomHeight() , heightMeasureSpec);
        //int heightSize = View.resolveSize(height , heightMeasureSpec);

        //MUST call this to store the measurements
        setMeasuredDimension(widthSize, heightSize);


    } else {
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }
}
}

我的自定义视图XML:

<demo.example.com.customarrayadapter.customviews.AspectRatioImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/imageView1"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="false"
            android:src="@drawable/icecream"
            android:adjustViewBounds="true"
            android:layout_gravity="top"
            android:foregroundGravity="top"/>

谢谢。

0 个答案:

没有答案