activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
tools:context="com.samset.samsetsample.MainActivity">
<com.samset.samsetsample.custom_view.ImageMovingView
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
tools:context="com.samset.samsetsample.MainActivity">
<com.samset.samsetsample.custom_view.ImageMovingView
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
ActvityMain.java
package com.samset.samsetsample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
ImageMovingView.java
package com.samset.samsetsample.custom_view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.samset.samsetsample.R;
/**
* Created by samset on 07/04/16.
*/
public class ImageMovingView extends View {
Paint paint;
Bitmap bm;
int bm_offsetX, bm_offsetY;
Path path;
PathMeasure pathMeasure;
float pathLength;
float step; //distance each step
float distance; //distance moved
float[] pos;
float[] tan;
Matrix matrix;
public ImageMovingView(Context context) {
super(context);
initMyView();
}
public ImageMovingView(Context context, AttributeSet attrs) {
super(context, attrs);
initMyView();
}
public ImageMovingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initMyView();
}
public void initMyView(){
paint = new Paint();
paint.setColor(Color.BLUE);// chenge color white
paint.setStrokeWidth(0);
paint.setStyle(Paint.Style.STROKE);
bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
bm_offsetX = bm.getWidth()/2;
bm_offsetY = bm.getHeight()/2;
path = new Path();
//path.moveTo(300, 100);
RectF rectF= new RectF(-100,150,700, 1250);
path.arcTo(rectF, -70, 100);
path.close();
pathMeasure = new PathMeasure(path, false);
pathLength = pathMeasure.getLength();
// Toast.makeText(getContext(), "pathLength: " + pathLength, Toast.LENGTH_LONG).show();
step = 1;
distance = 0;
pos = new float[2];
tan = new float[2];
matrix = new Matrix();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
if(distance < pathLength){
pathMeasure.getPosTan(distance, pos, tan);
matrix.reset();
float degrees = (float)(Math.atan2(tan[1], tan[0])*180.0/Math.PI);
matrix.postRotate(degrees, bm_offsetX, bm_offsetY);
matrix.postTranslate(pos[0] - bm_offsetX, pos[1] - bm_offsetY);
canvas.drawBitmap(bm, matrix, null);
distance += step;
if (distance>=800)
{
distance = 0;
invalidate();
}
Log.e("View"," distance "+distance);
}else{
distance = 0;
}
invalidate();
}
}
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.samset.samsetsample.R;
/**
* Created by samset on 07/04/16.
*/
public class ImageMovingView extends View {
Paint paint;
Bitmap bm;
int bm_offsetX, bm_offsetY;
Path path;
PathMeasure pathMeasure;
float pathLength;
float step; //distance each step
float distance; //distance moved
float[] pos;
float[] tan;
Matrix matrix;
public ImageMovingView(Context context) {
super(context);
initMyView();
}
public ImageMovingView(Context context, AttributeSet attrs) {
super(context, attrs);
initMyView();
}
public ImageMovingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initMyView();
}
public void initMyView(){
paint = new Paint();
paint.setColor(Color.BLUE);// chenge color white
paint.setStrokeWidth(0);
paint.setStyle(Paint.Style.STROKE);
bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
bm_offsetX = bm.getWidth()/2;
bm_offsetY = bm.getHeight()/2;
path = new Path();
//path.moveTo(300, 100);
RectF rectF= new RectF(-100,150,700, 1250);
path.arcTo(rectF, -70, 100);
path.close();
pathMeasure = new PathMeasure(path, false);
pathLength = pathMeasure.getLength();
// Toast.makeText(getContext(), "pathLength: " + pathLength, Toast.LENGTH_LONG).show();
step = 1;
distance = 0;
pos = new float[2];
tan = new float[2];
matrix = new Matrix();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
if(distance < pathLength){
pathMeasure.getPosTan(distance, pos, tan);
matrix.reset();
float degrees = (float)(Math.atan2(tan[1], tan[0])*180.0/Math.PI);
matrix.postRotate(degrees, bm_offsetX, bm_offsetY);
matrix.postTranslate(pos[0] - bm_offsetX, pos[1] - bm_offsetY);
canvas.drawBitmap(bm, matrix, null);
distance += step;
if (distance>=800)
{
distance = 0;
invalidate();
}
Log.e("View"," distance "+distance);
}else{
distance = 0;
}
invalidate();
}
}
Thank you
No comments:
Post a Comment