これを使用して、zoominglimitsとboundaruy limitsを指定することで、画面内でimageviewをドラッグしてズームできますが、複数の画像に使用したいので、main.xmlのcustomviewを使用しましたが、問題は、findviewbyIdがnullを返すことです。問題になる可能性がありますか?私はここで何が欠けていますか?PSを助けてください:-2ヶ月前のAndroid開発者です
NewzoomlimitActivity .java
public class NewzoomlimitActivity extends Activity
{
/** Called when the activity is first created. */
int h, w;
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
Display display = getWindowManager().getDefaultDisplay();
h = display.getHeight();
w = display.getWidth();
CustomTouchImageView img1 = new CustomTouchImageView( NewzoomlimitActivity.this);
CustomTouchImageView img11 = new CustomTouchImageView( NewzoomlimitActivity.this);
CustomTouchImageView img = ( CustomTouchImageView ) findViewById(R.id.gIFView1 );
Bitmap snoop = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher );
if ( null == img )
{
Log.e( "img", "img is null" );
}
else
{
img.setImage( snoop, w, h );
}
//img1.setImage( snoop, w, h );
//img11.setImage( snoop, w, h );
img.setImage( snoop, w, h );
//setContentView( img1 );
// setContentView( img11 );
setContentView( R.layout.main );
}
}
main.xml
---------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.coders.vimek.CustomTouchImageView
android:id="@+id/gIFView1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:src="@drawable/icon"
/>
<com.coders.vimek.CustomTouchImageView
android:id="@+id/gIFView2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>
</LinearLayout>
CustomTouchImageView .java
-----------------------------
public class CustomTouchImageView extends ImageView
{
private static final String TAG = "Touch";
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
int marg_left = 80, marg_right = 80, marg_top = 100, marg_botom = 130, win_w, win_h;
Bitmap bmp_picture;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
Context context;
/*
* public TouchImageView(Context context, AttributeSet attributeSet) {
* super(context, attributeSet);
*
* //TODO: }
*/
public CustomTouchImageView( Context context )
{
super( context );
super.setClickable( true );
this.context = context;
init();
}
public CustomTouchImageView( Context context, AttributeSet attrs )
{
super( context, attrs );
super.setClickable( true );
this.context = context;
init();
return;
}
public CustomTouchImageView( Context context, AttributeSet attrs, int defStyle )
{
super( context, attrs, defStyle );
super.setClickable( true );
this.context = context;
init();
}
private void init()
{
matrix.setTranslate( 1f, 1f );
setImageMatri(angry) matrix );
setScaleType( ScaleType.MATRIX );
setOnTouchListener( new OnTouchListener()
{
@Override
public boolean onTouch( View v, MotionEvent rawEvent )
{
WrapMotionEvent event = WrapMotionEvent.wrap( rawEvent );
// Dump touch event to log
// if (Viewer.isDebug == true){
// dumpEvent(event);
// }
// Handle touch events here...
switch ( event.getAction() & MotionEvent.ACTION_MASK )
{
case MotionEvent.ACTION_DOWN:
savedMatrix.set( matrix );
start.set( event.getX(), event.getY() );
Log.d( TAG, "mode=DRAG" );
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing( event );
Log.d( TAG, "oldDist=" + oldDist );
if ( oldDist > 10f )
{
savedMatrix.set( matrix );
midPoint( mid, event );
mode = ZOOM;
Log.d( TAG, "mode=ZOOM" );
}
break;
case MotionEvent.ACTION_UP:
int xDiff = ( int ) Math.abs( event.getX() - start.x );
int yDiff = ( int ) Math.abs( event.getY() - start.y );
if ( xDiff < 8 && yDiff < 8 )
{
performClick();
}
case MotionEvent.ACTION_POINTER_UP:
// //////////////////////////////////////
// You need this first:
double max_zoom = 3,
min_zoom = 0.4;
float f[] = new float[ 9 ];
matrix.getValues( f );
if ( null == bmp_picture )
{
Log.e( "bmp_picture", "bmp is null" );
}
/*
* else if ( null == bmp_picture.getWidth() ) {} else if (
* null == bmp_picture ) {}
*/
else
{
if ( f[ 0 ] > max_zoom )
{
matrix.postScale( ( float ) max_zoom / f[ 0 ], ( float ) max_zoom / f[ 0 ], mid.x, mid.y );
}
if ( f[ 0 ] < min_zoom )
{
matrix.postScale( ( float ) min_zoom / f[ 0 ], ( float ) min_zoom / f[ 0 ], mid.x, mid.y );
}
if ( -f[ 2 ] > ( bmp_picture.getWidth() * f[ 0 ] - marg_left ) )
{
matrix.postTranslate( -( f[ 2 ] + bmp_picture.getWidth() * f[ 0 ] ) + marg_left, 0 );
}
if ( -f[ 5 ] > ( bmp_picture.getHeight() * f[ 0 ] - marg_top ) )
{
matrix.postTranslate( 0, -( f[ 5 ] + bmp_picture.getHeight() * f[ 0 ] ) + marg_top );
}
if ( f[ 2 ] > win_w - marg_right )
{
matrix.postTranslate( -( f[ 2 ] - win_w + marg_right ), 0 );
}
if ( f[ 5 ] > win_h - marg_botom )
{
matrix.postTranslate( 0, -( f[ 5 ] - win_h + marg_botom ) );
}
}
// //////////////////////////////////////
mode = NONE;
Log.d( TAG, "mode=NONE" );
break;
case MotionEvent.ACTION_MOVE:
if ( mode == DRAG )
{
// ...
matrix.set( savedMatrix );
matrix.postTranslate( event.getX() - start.x, event.getY() - start.y );
}
else if ( mode == ZOOM )
{
float newDist = spacing( event );
Log.d( TAG, "newDist=" + newDist );
if ( newDist > 10f )
{
matrix.set( savedMatrix );
float scale = newDist / oldDist;
matrix.postScale( scale, scale, mid.x, mid.y );
}
}
break;
}
setImageMatri(angry) matrix );
return true; // indicate event was handled
}
} );
}
public void setImage( Bitmap bm, int displayWidth, int displayHeight )
{
super.setImageBitmap( bm );
Log.e( "win_w", "2" );
bmp_picture = bm;
win_h = displayHeight;
win_w = displayWidth;
Log.e( "win_h", String.valueOf( win_h ) );
Log.e( "win_w", String.valueOf( win_w ) );
float scale;
if ( ( displayHeight / bm.getHeight() ) >= ( displayWidth / bm.getWidth() ) )
{
scale = ( float ) displayWidth / ( float ) bm.getWidth();
}
else
{
scale = ( float ) displayHeight / ( float ) bm.getHeight();
}
savedMatrix.set( matrix );
matrix.set( savedMatrix );
matrix.postScale( scale, scale, mid.x, mid.y );
setImageMatri(angry) matrix );
// Center the image
float redundantYSpace = ( float ) displayHeight - ( scale * ( float ) bm.getHeight() );
float redundantXSpace = ( float ) displayWidth - ( scale * ( float ) bm.getWidth() );
redundantYSpace /= ( float ) 2;
redundantXSpace /= ( float ) 2;
savedMatrix.set( matrix );
matrix.set( savedMatrix );
matrix.postTranslate( redundantXSpace, redundantYSpace );
setImageMatri(angry) matrix );
}
/** Show an event in the LogCat view, for debugging */
@SuppressWarnings( "unused" )
private void dumpEvent( WrapMotionEvent event )
{
String names[] =
{ "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append( "event ACTION_" ).append( names[ actionCode ] );
if ( actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP )
{
sb.append( "(pid " ).append( action >> MotionEvent.ACTION_POINTER_ID_SHIFT );
sb.append( ")" );
}
sb.append( "[" );
for ( int i = 0; i < event.getPointerCount(); i++ )
{
sb.append( "#" ).append( i );
sb.append( "(pid " ).append( event.getPointerId( i ) );
sb.append( ")=" ).append( ( int ) event.get(angry) i ) );
sb.append( "," ).append( ( int ) event.getY( i ) );
if ( i + 1 < event.getPointerCount() )
sb.append( ";" );
}
sb.append( "]" );
Log.d( TAG, sb.toString() );
}
/** Determine the space between the first two fingers */
private float spacing( WrapMotionEvent event )
{
float x = event.get(angry) 0 ) - event.get(angry) 1 );
float y = event.getY( 0 ) - event.getY( 1 );
return FloatMath.sqrt( x * x + y * y );
}
/** Calculate the mid point of the first two fingers */
private void midPoint( PointF point, WrapMotionEvent event )
{
float x = event.get(angry) 0 ) + event.get(angry) 1 );
float y = event.getY( 0 ) + event.getY( 1 );
point.set( x / 2, y / 2 );
}
}