变色球

package com.example.moveball;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {

	ImageView image, image2;
	Button up;
	Button down;
	Ball ball;
	int x, y, w, h, vw, vh;
	Bitmap bitmap;
	BitmapDrawable bit;
	Double scale;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		image = (ImageView) findViewById(R.id.image);
		image2 = (ImageView) findViewById(R.id.image2);
		up = (Button) findViewById(R.id.up);
		down = (Button) findViewById(R.id.down);
		ball = (Ball) findViewById(R.id.exercise_ball);
		y = ball.cy;
		x = ball.cx;
		bit = (BitmapDrawable) image.getDrawable();
		bitmap = bit.getBitmap();

		up.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Scale();
				if (ball.cy > 0) {
					ball.cy = ball.cy - 10;
				}
				y = ball.cy;
				x = (int) (ball.cx / scale);
				y = (int) (y / scale);
				int i = bitmap.getPixel(x, y);
				if (i == 0) {
					ball.change = 1;
				} else {
					ball.change = 0;
				}
				ball.invalidate();
			}
		});
		down.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Scale();
				if (ball.cy < 80) {
					ball.cy = ball.cy + 10;
				}
				y = ball.cy;
				x = (int) (ball.cx / scale);
				y = (int) (ball.cy / scale);
				int i = bitmap.getPixel(x, y);
				if (i == 0) {
					ball.change = 1;
				} else {
					ball.change = 0;
				}

				ball.invalidate();
			}

		});
	}

	public void Scale() {
		double m = image.getWidth();
		double n = bitmap.getWidth();
		scale = m / n;
	}
}
package com.example.moveball;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

@SuppressLint("DrawAllocation")
public class Ball extends View {
	
	public Ball(Context context, AttributeSet attrs) {
		super(context, attrs);
		cx = 200;
		cy = 10;
	}

	public int cx;
	public int cy;
	public static int change =0;

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint = new Paint();
		paint.setColor(Color.GREEN);
		if(change==1){
			paint.setColor(Color.RED);
		} 
		canvas.drawCircle(cx, cy, 15, paint);
	}
}
<TableLayout 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:orientation="vertical"
    tools:context="${packageName}.${activityClass}" >

    <TableRow>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="150sp"
            tools:context="${packageName}.${activityClass}" 
            android:background="@drawable/span">

            <ImageView
                android:id="@+id/image"
                android:layout_width="300dp"
                android:layout_height="100dp"
                android:src="@drawable/add_force"
                android:background="@drawable/span" />

            <com.example.moveball.Ball
                android:id="@+id/exercise_ball"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </FrameLayout>
    </TableRow>

    <TableRow>

        <Button
            android:id="@+id/up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="小球上移" />
    </TableRow>

    <TableRow>

        <Button
            android:id="@+id/down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="小球下移" />
    </TableRow>

    <ImageView
        android:id="@+id/image2"
        
        />

</TableLayout>

以上为主要代码,MainActivity主要做的工作为获取ImageView的Bitmap,通过对小球圆心坐标(容器中坐标系)转化到Bitmap的坐标,首先获取到的Bitmap要获取到它的缩放比从而实现坐标的转化,转化后只需要调用Bitmap.getPixel(x,y)即可获得颜色判断条件,从而实现小球颜色随着运动是否按轨迹而变化。

后边为自定义小球代码与布局代码。

你可能感兴趣的:(android,;,;,Bitmap.getPixel,(x,y))