从主 activity 调用自定义视图.. 打印 onDraw() 中的消息但未呈现自定义视图
Calling custom view from main activity.. Message in onDraw() is printed but custom view is not rendered
我有两个 类 - MainActivity.java 和 MainView.java。
当从主 activity.
触摸屏幕时,我正在尝试呈现自定义视图(在 MainView.java 中定义)
我的代码正在做一件时髦的事情。从主页上,当您触摸屏幕时,它不会呈现我在 MainView.java 中指定的自定义视图 - 但仍会打印出我在 onDraw() 方法中输入的消息,这应该意味着 onDraw( ) 正在 运行。为什么我的 onDraw() 方法是 运行 但它的视图不呈现?
此外,我在 MainActivity.java 和 MainView.java 中的 onTouchEvent 似乎同时响应,尽管我看到的视图是唯一的 activity_main.xml.
请帮忙:(
MainActivity.java
package com.example.owner.thesisapp;
import com.example.owner.thesisapp.MainView;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.media.AudioManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.media.SoundPool;
import android.view.View;
import android.graphics.Canvas;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends Activity {
private static SoundPool soundPool;
private static HashMap soundPoolMap;
private static int S1 = R.raw.a3v1;
private static int S2 = R.raw.c3v1;
private static int S3 = R.raw.dsharp3v1;
private static int S4 = R.raw.fsharp3v1;
private int soundID;
private boolean isSoundLoaded = false;
public MainView mainView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainView = (MainView) findViewById(R.id.main_view);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
isSoundLoaded = true;
}
});
soundID = soundPool.load(this, R.raw.fsharp3v1, 1);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
if (isSoundLoaded) {
soundPool.play(soundID, 1f, 1f, 1, 0, 1f);
Log.d("Test", "Played sound");
}
mainView.postInvalidate();
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
// Keep landscape orientation
public void onConfigurationChanged(Configuration newConfig) {
// ignore orientation/keyboard change
super.onConfigurationChanged(newConfig);
}
}
MainView.java
package com.example.owner.thesisapp;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
/**
* Created by Owner on 6/1/2015.
*/
public class MainView extends View {
Bitmap stillWave;
int iy = 0;
public MainView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
stillWave = BitmapFactory.decodeResource(getResources(), R.drawable.wave);
setWillNotDraw(false);
Log.d("MAINVIEW: Is this ran?", " ");
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("Action DOWN!", " ");
iy = 0;
this.invalidate();
case MotionEvent.ACTION_MOVE:
Log.d("Action MOVE!", " ");
case MotionEvent.ACTION_UP:
Log.d("Action UP!", " ");
}
return false;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d("MAINVIEW ONDRAW: Is this ran?", " ");
int width = canvas.getWidth();
int height = canvas.getHeight();
//set background
Rect wBackground = new Rect();
wBackground.set(0, 0, width, height);
Paint pBackground = new Paint();
pBackground.setStyle(Paint.Style.FILL);
pBackground.setColor(Color.BLACK);
canvas.drawRect(wBackground, pBackground);
//Log.d("garo, sero", width + " " + height);
//set Wave
Paint pWave = new Paint();
pWave.setStyle(Paint.Style.FILL_AND_STROKE);
pWave.setColor(Color.WHITE);
pWave.setStrokeWidth(10);
pWave.setStrokeCap(Paint.Cap.ROUND);
int waveFlucMin = -100;
int waveFlucMax = 100;
float[] coords = new float[width * 2];
for (int ix = 0; ix < width; ix++) {
int yoffset = waveFlucMin + (int) (Math.random() * ((waveFlucMax - waveFlucMin) + 1));
coords[2 * ix] = (float) ix;
coords[2 * ix + 1] = (float) iy + yoffset;
}
//Log.d("Coordinates x?", coords[1] + " " + coords[3] + " " + coords[5] + " " + coords[7] + " " + coords[301] + " " + coords[303] + " ");
//Log.d("Coordinates y?", coords[0] + " " + coords[2] + " " + coords[4] + " " + coords[6] + " " + coords[300] + " " + coords[302] + " ");
//Log.d("Generating?", "YES, generating forloop : " + iy);
//wait a second between each wave generation
/*
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
*/
drawWave(coords, width, pWave, canvas);
if (iy < height) {
iy += 10;
invalidate();
} else {
//do nothing - finish onDraw() call.
}
}
private void drawWave(float[] coords, int width, Paint pWave, Canvas canvas) {
canvas.drawPoints(coords, 0, width*2, pWave);
}
}
activity_main.xml
<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" tools:context=".MainActivity">
<com.example.owner.thesisapp.MainView
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#111C64"/>
问题出在你的布局上。由于您使用的是 RelativeLayout
,因此您的自定义 View
和 ImagView
都与左上角对齐。在这种情况下,z 顺序将 ImageView
放在自定义 View
的顶部,并且由于它占据了整个屏幕,因此您看不到自定义视图。
我有两个 类 - MainActivity.java 和 MainView.java。
当从主 activity.
触摸屏幕时,我正在尝试呈现自定义视图(在 MainView.java 中定义)我的代码正在做一件时髦的事情。从主页上,当您触摸屏幕时,它不会呈现我在 MainView.java 中指定的自定义视图 - 但仍会打印出我在 onDraw() 方法中输入的消息,这应该意味着 onDraw( ) 正在 运行。为什么我的 onDraw() 方法是 运行 但它的视图不呈现?
此外,我在 MainActivity.java 和 MainView.java 中的 onTouchEvent 似乎同时响应,尽管我看到的视图是唯一的 activity_main.xml.
请帮忙:(
MainActivity.java
package com.example.owner.thesisapp;
import com.example.owner.thesisapp.MainView;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.media.AudioManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.media.SoundPool;
import android.view.View;
import android.graphics.Canvas;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends Activity {
private static SoundPool soundPool;
private static HashMap soundPoolMap;
private static int S1 = R.raw.a3v1;
private static int S2 = R.raw.c3v1;
private static int S3 = R.raw.dsharp3v1;
private static int S4 = R.raw.fsharp3v1;
private int soundID;
private boolean isSoundLoaded = false;
public MainView mainView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainView = (MainView) findViewById(R.id.main_view);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
isSoundLoaded = true;
}
});
soundID = soundPool.load(this, R.raw.fsharp3v1, 1);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
if (isSoundLoaded) {
soundPool.play(soundID, 1f, 1f, 1, 0, 1f);
Log.d("Test", "Played sound");
}
mainView.postInvalidate();
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
// Keep landscape orientation
public void onConfigurationChanged(Configuration newConfig) {
// ignore orientation/keyboard change
super.onConfigurationChanged(newConfig);
}
}
MainView.java
package com.example.owner.thesisapp;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
/**
* Created by Owner on 6/1/2015.
*/
public class MainView extends View {
Bitmap stillWave;
int iy = 0;
public MainView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
stillWave = BitmapFactory.decodeResource(getResources(), R.drawable.wave);
setWillNotDraw(false);
Log.d("MAINVIEW: Is this ran?", " ");
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("Action DOWN!", " ");
iy = 0;
this.invalidate();
case MotionEvent.ACTION_MOVE:
Log.d("Action MOVE!", " ");
case MotionEvent.ACTION_UP:
Log.d("Action UP!", " ");
}
return false;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d("MAINVIEW ONDRAW: Is this ran?", " ");
int width = canvas.getWidth();
int height = canvas.getHeight();
//set background
Rect wBackground = new Rect();
wBackground.set(0, 0, width, height);
Paint pBackground = new Paint();
pBackground.setStyle(Paint.Style.FILL);
pBackground.setColor(Color.BLACK);
canvas.drawRect(wBackground, pBackground);
//Log.d("garo, sero", width + " " + height);
//set Wave
Paint pWave = new Paint();
pWave.setStyle(Paint.Style.FILL_AND_STROKE);
pWave.setColor(Color.WHITE);
pWave.setStrokeWidth(10);
pWave.setStrokeCap(Paint.Cap.ROUND);
int waveFlucMin = -100;
int waveFlucMax = 100;
float[] coords = new float[width * 2];
for (int ix = 0; ix < width; ix++) {
int yoffset = waveFlucMin + (int) (Math.random() * ((waveFlucMax - waveFlucMin) + 1));
coords[2 * ix] = (float) ix;
coords[2 * ix + 1] = (float) iy + yoffset;
}
//Log.d("Coordinates x?", coords[1] + " " + coords[3] + " " + coords[5] + " " + coords[7] + " " + coords[301] + " " + coords[303] + " ");
//Log.d("Coordinates y?", coords[0] + " " + coords[2] + " " + coords[4] + " " + coords[6] + " " + coords[300] + " " + coords[302] + " ");
//Log.d("Generating?", "YES, generating forloop : " + iy);
//wait a second between each wave generation
/*
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
*/
drawWave(coords, width, pWave, canvas);
if (iy < height) {
iy += 10;
invalidate();
} else {
//do nothing - finish onDraw() call.
}
}
private void drawWave(float[] coords, int width, Paint pWave, Canvas canvas) {
canvas.drawPoints(coords, 0, width*2, pWave);
}
}
activity_main.xml
<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" tools:context=".MainActivity">
<com.example.owner.thesisapp.MainView
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#111C64"/>
问题出在你的布局上。由于您使用的是 RelativeLayout
,因此您的自定义 View
和 ImagView
都与左上角对齐。在这种情况下,z 顺序将 ImageView
放在自定义 View
的顶部,并且由于它占据了整个屏幕,因此您看不到自定义视图。