天天看點

Android Api Demos登頂之路(八十八)Graphics-->ScaleToFit

/*
 * Matrix的setRectToRect(RectF src, RectF dst, Matrix.ScaleToFit stf)方法實作了從源圖形向目标區域圖形的變換。
 * Matrix.ScaleToFit參數定義了四種縮放選項
 * 1.CENTER: 保持坐标變換前矩形的長寬比,并最大限度的填充變換後的矩形。至少有一邊和目标矩形重疊,原圖形在目标圖形中居中顯示。
 * 2.END:保持坐标變換前矩形的長寬比,并最大限度的填充變換後的矩形。至少有一邊和目标矩形重疊。END提供右下對齊。
 * 3.FILL: 可能會變換矩形的長寬比,保證變換和目标矩陣長寬一緻。
 * 4.START:保持坐标變換前矩形的長寬比,并最大限度的填充變換後的矩形。至少有一邊和目标矩形重疊。START提供左上對齊。
 */
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }

    private static class SampleView extends View{
        private Paint mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        private Paint mStrokePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        private Paint mLabelPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        private Matrix mMatrix=new Matrix();
        private RectF mSrc=new RectF();

        //定義縮放模式
        private static final Matrix.ScaleToFit[] sFits=new Matrix.ScaleToFit[]{
                Matrix.ScaleToFit.CENTER,
                Matrix.ScaleToFit.END,
                Matrix.ScaleToFit.FILL,
                Matrix.ScaleToFit.START,
        };

        private static final String[] sFitLabels=new String[]{
            "CENTER","END","FILL","START"
        };

        //定義源圖形的大小及顔色
        private static final int[] srcData=new int[]{
            ,,Color.RED,
            ,,Color.GREEN,
            ,,Color.BLUE,
            ,,Color.BLACK,
        };

        private int N=;
        private static final int WIDTH=;
        private static final int HEIGHT=;
        //定義目标區域
        private RectF mDest=new RectF(,,WIDTH,HEIGHT);

        public SampleView(Context context) {
            super(context);
            mStrokePaint.setStyle(Paint.Style.STROKE);
            mLabelPaint.setTextSize();
        }

        //設定源圖形
        private void setSrc(int index){
            int w=srcData[index*];
            int h=srcData[index*+];
            //設定源圖形的區域
            mSrc.set(, , w, h);
        }

        private void drawSrc(Canvas canvas,int index){
            mPaint.setColor(srcData[index*+]);
            canvas.drawOval(mSrc, mPaint);
        }

        //按綻放模式将源圖形轉換為目标區域内的圖形
        private void drawFit(Canvas canvas,int index,Matrix.ScaleToFit fit){
            canvas.save();
            setSrc(index);
            mMatrix.setRectToRect(mSrc, mDest, fit);
            canvas.concat(mMatrix);
            drawSrc(canvas, index);
            canvas.restore();

            canvas.drawRect(mDest, mStrokePaint);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawColor(Color.WHITE);
            canvas.translate(, );

            canvas.save();
            //繪制源圖形
            for(int i=;i<N;i++){
                setSrc(i);
                drawSrc(canvas, i);
                canvas.translate(mSrc.width()+, );
            }
            canvas.restore();

            canvas.translate(, );
            for(int i=;i<sFits.length;i++){
                canvas.save();
                for(int j=;j<N;j++){
                    drawFit(canvas, j, sFits[i]);
                    canvas.translate(mDest.width()+, );
                }
                canvas.drawText(sFitLabels[i], , HEIGHT*/, mLabelPaint);
                canvas.restore();
                canvas.translate(, );
            }
        }
    }
}