Last active
January 22, 2018 08:59
-
-
Save catvinhquang/2fbf31f689079ff6539d2d6f1fbc11a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.quangcv.myapplication; | |
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.graphics.Bitmap; | |
| import android.graphics.BitmapFactory; | |
| import android.graphics.BitmapShader; | |
| import android.graphics.Canvas; | |
| import android.graphics.Matrix; | |
| import android.graphics.Paint; | |
| import android.graphics.Shader; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| public class MainActivity extends Activity { | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(new MyView(this)); | |
| } | |
| class MyView extends View { | |
| BitmapShader shader; | |
| Paint paint; | |
| int scrW, scrH; | |
| int radius; | |
| Bitmap bitmap; | |
| public MyView(Context context) { | |
| super(context); | |
| scrW = getResources().getDisplayMetrics().widthPixels; | |
| scrH = getResources().getDisplayMetrics().heightPixels; | |
| radius = scrW / 2; | |
| bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.baby); | |
| shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); | |
| final Matrix matrix = new Matrix(); | |
| float scale = scrW / bitmap.getWidth(); | |
| matrix.setScale(scale, scale); | |
| shader.setLocalMatrix(matrix); | |
| paint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
| paint.setShader(shader); | |
| } | |
| @Override | |
| protected void onDraw(Canvas canvas) { | |
| canvas.save(); | |
| canvas.translate(0, scrH - radius * 2); | |
| canvas.drawCircle(radius, radius, radius, paint); | |
| canvas.restore(); | |
| canvas.drawBitmap(bitmap, 0, 0, paint); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

baby.png
